移至 Express 4
概觀
Express 4 是對 Express 3 的突破性變更。也就是說,如果您在其相依關係中更新 Express 版本,現有的 Express 3 應用程式將無法運作。 That means an existing Express 3 app will not work if you update the Express version in its dependencies.
本文涵蓋:
- Express 4 中的變更。
- 將 Express 3 應用程式移轉至 Express 4 的範例。
- 升級至 Express 4 應用程式產生器。
Express 4 中的變更
Express 4 有數項明顯的變更:
- Changes to Express core and middleware system. The dependencies on Connect and built-in middleware were removed, so you must add middleware yourself.
- 路由系統的變更。
- 其他各項變更。
另請參閱:
Express 核心和中介軟體系統的變更
Express 4 不再相依於 Connect,除了 express.static
函數,其他所有的內建中介軟體皆已從其核心移除。也就是說,Express 現在是一個獨立的路由與中介軟體 Web 架構,Express 的版本化與版次不受中介軟體更新的影響。 This means that
Express is now an independent routing and middleware web framework, and
Express versioning and releases are not affected by middleware updates.
由於沒有內建中介軟體,您必須明確新增執行您應用程式所需的所有中介軟體。只需遵循下列步驟: Simply follow these steps:
- 安裝模組:
npm install --save <module-name>
- 在您的應用程式中,需要模組:
require('module-name')
- 遵循模組的說明文件來使用該模組:
app.use( ... )
下表列出 Express 3 中介軟體和其在 Express 4 中的對應項目。
Express 3 | Express 4 |
---|---|
express.bodyParser |
body-parser + multer |
express.compress |
compression |
express.cookieSession |
cookie-session |
express.cookieParser |
cookie-parser |
express.logger |
morgan |
express.session |
express-session |
express.favicon |
serve-favicon |
express.responseTime |
response-time |
express.errorHandler |
errorhandler |
express.methodOverride |
method-override |
express.timeout |
connect-timeout |
express.vhost |
vhost |
express.csrf |
csurf |
express.directory |
serve-index |
express.static |
serve-static |
以下是 Express 4 中介軟體的完整清單。
In most cases, you can simply replace the old version 3 middleware with its Express 4 counterpart. For details, see the module documentation in GitHub.
app.use
接受參數
In version 4 you can use a variable parameter to define the path where middleware functions are loaded, then read the value of the parameter from the route handler. For example:
app.use('/book/:id', (req, res, next) => {
console.log('ID:', req.params.id)
next()
})
路由系統
Apps 現在隱含地載入了路由中介軟體,因此您不用再擔心該中介軟體相對於 router
中介軟體的載入順序。
路由的定義方式不變,但是路由系統多了兩個新特性,可協助您組織路由:
- 新方法
app.route()
,用來為路由路徑建立可鏈接的路由處理程式。 - 新類別
express.Router
,用來建立可裝載的模組路由處理程式。
app.route()
方法
新的 app.route()
方法可讓您為路由路徑建立可鏈接的路由處理程式。由於是在單一位置指定路徑,建立模組路由很有用,因為它可減少冗餘和打錯字的情況。如需路由的相關資訊,請參閱 Router()
說明文件。 Because the path is specified in a single location, creating modular routes is helpful, as is reducing redundancy and typos. For more
information about routes, see Router()
documentation.
下列範例顯示利用 app.route()
函數所定義的路由處理程式鏈。
app.route('/book')
.get((req, res) => {
res.send('Get a random book')
})
.post((req, res) => {
res.send('Add a book')
})
.put((req, res) => {
res.send('Update the book')
})
express.Router
類別
The other feature that helps to organize routes is a new class,
express.Router
, that you can use to create modular mountable
route handlers. A Router
instance is a complete middleware and
routing system; for this reason it is often referred to as a “mini-app”.
下列範例是將路由器建立成模組、 在其中載入中介軟體、定義一些路由,並將它裝載在主要應用程式中的路徑。
例如,在應用程式目錄中建立一個名為 birds.js
的路由器檔案,內含下列內容:
var express = require('express')
var router = express.Router()
// middleware specific to this router
router.use((req, res, next) => {
console.log('Time: ', Date.now())
next()
})
// define the home page route
router.get('/', (req, res) => {
res.send('Birds home page')
})
// define the about route
router.get('/about', (req, res) => {
res.send('About birds')
})
module.exports = router
然後將路由器模組載入應用程式中:
var birds = require('./birds')
// ...
app.use('/birds', birds)
現在,應用程式就能夠處理發給 /birds
和 /birds/about
路徑的要求,並且會呼叫該路由特定的 timeLog
中介軟體。
其他變更
下表列出 Express 4 其他小幅卻很重要的變更:
Object | Description |
---|---|
Node.js | Express 4 需要 Node.js 0.10.x 或更新版本,且不再支援 Node.js 0.8.x。 |
|
不再需要 |
|
The |
|
Express 4 中依預設會停用 |
|
使用 |
|
不再解析相對 URL。 |
|
Was an array; now an object. |
|
Was a function; now an object. |
|
已變更為 |
|
現在以 |
|
已移除。 |
|
已移除。 |
|
Functionality is now limited to setting the basic cookie value.
現在功能僅限於設定基本 Cookie 值。請使用
|