express 基于 Node.js 平台,快速、开放、极简的 Web 开发框架
文档
安装
1 | npm install express |
启动服务
1 | const server = require('express')(); |
router
app.METHOD(PATH, HANDLER)
- app is an instance of express.
- METHOD is an HTTP request method, in lowercase.
- PATH is a path on the server.
- HANDLER is the function executed when the route is matched.
1
2
3app.get('/', function (req, res) {
res.send('hello world')
})
middleware 中间件
use
1 | var myLogger = function (req, res, next) { |
Built-in middleware
- express.static serves static assets such as HTML files, images, and so on.
- express.json parses incoming requests with JSON payloads. NOTE: Available with Express 4.16.0+
- express.urlencoded parses incoming requests with URL-encoded payloads. NOTE: Available with Express 4.16.0+
1 | let options = { |
模板 template engines
pug
1 | npm install pug |
1 | //serve.js |
调试DEBUG
1 | # lunix: |
更多文档,请查看官方4x文档