express是基于node.js的web开发框架
基本应用
1 2 3 4 5 6 7 8 9 10 11
| const express = require('express') const app = express() const port = 3000
app.get('/' res.send('Hello World!') })
app.listen(port, () => { console.log(`Example app listening at http://localhost:${port}`) })
|
express不仅支持
中间件
中间件是对一次请求进行多次有次序的处理,打个比方我们要验证前端发来的数据,然后通过后发送登入成功,就可以用两个方法,一个验证前端数据,一个发送数据,因为验证是在发送数据前面,就要用到中间件
1 2 3 4 5 6 7 8 9 10 11 12 13
| var express = require('express'); var app = express(); app.get('/', function(req, res, next) { checkres next(); }); app.get('/end', function(req, res) { res.send('程序到我这里就结束了,没有next方法'); }) app.listen(3000);
|
使用next()就会调用下一个中间件
中间件还有种使用方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| var express = require('express'); var app = express(); var next= function(req, res, next) { next(); } app.use(next)
app.get('/end', function(req, res) { res.send('程序到我这里就结束了,没有next方法'); }) app.listen(3000);
|
api
太多了,不记了直接去官网看