0 修改記錄
時間 | 描述 |
---|---|
2019-04-08 | 建檔 |
1 實踐目的
- 初步瞭解NodeJS。
- 利用Express搭建簡單的Web應用後端。
注:本實踐假設讀者已正確安裝NodeJS。
2 創建項目
首先新建一個文件夾。例如,management
。在DOS界面中進入該文件夾,執行npm init
:
D:\...\mineblog>npm init
This utility will walk you through creating a package.json file.
It only covers the most common items, and tries to guess sensible defaults.
See `npm help json` for definitive documentation on these fields
and exactly what they do.
Use `npm install <pkg>` afterwards to install a package and
save it as a dependency in the package.json file.
Press ^C at any time to quit.
package name: (mineblog)
version: (1.0.0)
description: Personal Blog
entry point: (index.js) app.js
test command:
git repository:
keywords:
author: [email protected]
license: (ISC)
About to write to D:\Chngzhen\Programmes\NodeJS\mineblog\package.json:
{
"name": "mineblog",
"version": "1.0.0",
"description": "Personal Blog",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "[email protected]",
"license": "ISC"
}
Is this OK? (yes)
填寫的信息包括:
package name
:包名。必填,默認爲根目錄名稱。version
:版本號。必填,默認爲1.0.0。description
:描述。可選。entry point
:應用入口。必填,默認爲根目錄下的index.js
。test command
:可選git repository
:可選keywords
:可選author
:作者。可選license
:必填。默認爲ISC。
3 搭建框架
從粗略上講,Web應用分前端和後端。本文對前端不做詳細講解,主要關注利用NodeJS開發後端。
目前開發Web應用,一般都會使用成熟的框架。比如Java Web的Spring、Strut2,Python的Django等。對於NodeJS,比較常見的就是Express。
3.1 安裝Express
在項目根目錄運行npm insall express
。
D:\..\mineblog>npm install express --save
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN [email protected] No repository field.
+ [email protected]
added 48 packages from 36 contributors and audited 121 packages in 16.242s
found 0 vulnerabilities
注:原生的NPM下載速度雖然不快,但也不慢。國內也有相應的鏡像,可搜索相關資料。
3.2 使用Express
在項目根目錄下新建應用入口, 例如app.js
。
// 1. 導入Express模塊
var express = require('express');
// 2. 實例化Express
var app = express();
// 3. 處理請求:返回字符串
app.get('/', (req, res) => {
res.write('Hello, world!');
res.end();
});
// 4. 發佈應用:將應用發佈到8900端口
app.listen(8900, () => console.log('The application is running at http://127.0.0.1:8900'));
3.3 啓動應用
在項目根目錄下運行node app.js
:
D:\...\management>node app.js
The application is running at http://127.0.0.1:8900
訪問http://127.0.0.1:8900
即可看見Hello, world!
字符串。
4 Express的路由
一個複雜的Web應用必定有許多資源,考慮到代碼的可維護性,用戶對這些資源的請求不可能都放在app.js
文件中處理。因此像Spring MVC那樣的架構就成了必要:
- M,即業務模型(Model)。這裏暫時用不到。
- V,即視圖(View),指前端展示。
- C,即控制器(Controller),接收請求並調用業務模型處理。
例如,將與用戶相關的資源請求放在一個文件中處理。
// UserController.js - User相關的資源請求處理
var express = require('express'),
router = express.Router();
router.get('/', (req, res) => {
res.write('This is the Home page of User module.');
res.end();
});
module.exports = router;
將其它無法處理的請求放在另一個文件中處理。
// IndexController.js - 其它請求處理
var express = require('express'),
router = express.Router();
router.get('/', (req, res) => {
res.write('This is the Home page of site.');
res.end();
});
module.exports = router;
然後利用Express的路由器,將請求分發到不同的文件。
var express = require('express');
var app = express();
app.use('/user', require('./source/controller/UserController.js'));
app.use('/', require('./source/controller/IndexController.js'));
app.listen(8900, () => console.log('The application is running at http://127.0.0.1:8900'));