一、express入门
1. 介绍
- express是基于node.js平台,快速,开放,极简的web开发框架
- express和node.js内置的http模块类似,用来专门创建web服务器的
- express是第三方包,基于http封装,提供简单易用的api
2. 入门demo
- npm install express
- 运行后就回启动一个web服务器
/*导入*/
let express = require('express');
/*创建一个server*/
let server = express();
/*端口,ip,服务器启动后的回调函数*/
server.listen(8080, 'localhost', function () {
console.log('Server Start Successfully');
})
3. get/post请求
/*导入*/
let express = require('express');
/*创建一个server*/
let server = express();
/*端口,ip,服务器启动后的回调函数*/
server.listen(8080, 'localhost', function () {
console.log('Server Start Successfully');
})
/*get请求:
* 1. 具体的路径*/
server.get('/user', function (request, response) {
console.log(request);
/*api请求结果: 可以以json返回数据,也可以以普通文本返回*/
response.send({"name": "erick", "age": 18});
});
server.post('/info', function (request, response) {
console.log(request);
response.send("Hello Erick");
});
/*get请求,通过 query来获取参数
* http://localhost:8080/name?name=shuzhan&age=10*/
server.get('/name', function (request, response) {
let query = request.query;
console.log(query);
response.send(query);
});
/*get请求,通过动态参数拼接
* http://localhost:8080/queryById/2*/
server.get('/queryById/:id', function (request, response) {
let id = request.params;
console.log(id);
/*{ id: '2' }*/
response.send(id);
})
/*get请求多个参数:
http://localhost:8080/query/2/shuzhan*/
server.get('/query/:id/:name', function (request, response) {
let params = request.params;
console.log(params);
/*{ id: '2', name: 'shuzhan' }*/
response.send(params);
})
二、express静态资源托管
- express.static():将目录下的img,css,js对外公开访问
- 静态文件的目录名不会出现在url中
- 目录名和当前启动服务的js代码平级
let express = require('express');
let server = express();
server.listen(8080, 'localhost', function () {
console.log('Server Start Successfully');
})
/*将和本js文件平级目录下的,public目录下的img,css,js,html对外暴露
1.可以修改目录,需要注意找到对应的public路径
2.图片访问地址: http://localhost:8080/1.jpg
3.可以定一个多个静态资源路径
4. 从上往下,根据静态资源加载目录来查看对应的文件*/
server.use(express.static('../public'));
server.use(express.static('../static'));
/*静态资源访问时候,可以指定路径前缀
* 1. http://localhost:8080/nike/index.html*/
server.use('/nike', express.static('../entry'));
三、express路由
1. 直接挂载,如上所属
2. 路由模块化
userRouter.js
let express = require('express');
let userRouter = express.Router();
/*定义多个路由*/
userRouter.get('/name', function (request, response) {
response.send('User name : erick');
});
userRouter.get('/age', function (request, response) {
response.send('User age : 20');
});
/*不要用{}方式导出*/
module.exports = userRouter;
index.js
let express = require('express');
let userRouter = require('../router/userRouter');
let server = express();
server.listen(8080, 'localhost', function () {
console.log('Server Start Successfully');
})
/*使用当前导入的router
1. 可以挂载多个路由
2. 声明顺序决定路由加载顺序
3. 如果一个路由匹配到,后续则不再匹配*/
server.use(userRouter);
3. 路由模块添加统一前缀
let express = require('express');
let userRouter = require('../router/userRouter');
let server = express();
server.listen(8080, 'localhost', function () {
console.log('Server Start Successfully');
})
/*路由模块添加统一前缀
* http://localhost:8080/api/name*/
server.use('/api',userRouter);
四、express中间件
- 请求进入express服务后,对请求和响应预处理,类似filter
- 可以处理request或者response,链式处理或者自定义属性
1. 全局中间件
1.1 基本demo
let express = require('express');
let app = express();
app.listen(8080, 'localhost', function () {
console.log('server start successfully');
});
/*定义一个middleware
* 1. 一定是有next函数的,才是一个middleware
* 2. 请求进来后,先进入对应的middleware,再去执行对应的get或者post请求*/
function firstMiddleWare(request, response, next) {
console.log('First Middle Ware is coming!!!');
/*next:表示当前中间件执行完毕了,继续执行后续操作*/
next();
}
/*将中间件注册为全局middleware*/
app.use(firstMiddleWare);
app.get('/user', function (request, response) {
console.log('User Get method is executed');
})
1.2 多个中间件
let express = require('express');
let app = express();
app.listen(8080, 'localhost', function () {
console.log('server start successfully');
});
function firstMiddleWare(request, response, next) {
/*在中间件内部,可以自定义属性或者字段,后面的中间件,可以获取改属性*/
request.startTime = Date.now();
console.log('First Middle Ware is coming!!!');
next();
}
function secondMiddleWare(request, response, next) {
console.log(request.startTime);
console.log('Second Middle Ware is coming!!!');
next();
}
/*多个中间件执行顺序: 根据中间件的挂载顺序依次执行*/
app.use(firstMiddleWare);
app.use(secondMiddleWare);
app.get('/user', function (request, response) {
console.log('User Get method is executed');
})
2. 局部中间件
2.1 一个中间件
let express = require('express');
let app = express();
app.listen(8080, 'localhost', function () {
console.log('server start successfully');
});
function firstMiddleWare(request, response, next) {
request.startTime = Date.now();
console.log('First Middle Ware is coming!!!');
next();
}
/*局部中间件,不要在app上挂载,而是挂载到具体的方法里面*/
app.get('/user', firstMiddleWare, function (request, response) {
console.log(request.startTime);
console.log('User Get method is executed');
})
2.2 多个中间件
- [firstMiddleWare, secondMiddleWare]
- 或者firstMiddleWare,secondMiddleWare
let express = require('express');
let app = express();
app.listen(8080, 'localhost', function () {
console.log('server start successfully');
});
function firstMiddleWare(request, response, next) {
console.log('First Middle Ware is coming!!!');
next();
}
function secondMiddleWare(request, response, next) {
console.log('Second Middle Ware is coming!!!');
next();
}
/*局部中间件,不要在app上挂载,而是挂载到具体的方法里面*/
app.get('/user', [firstMiddleWare, secondMiddleWare], function (request, response) {
console.log('User Get method is executed');
})
3. 注意事项
- 中间件要在路由挂载前
- 可以声明多个中间件,多个中间件共享request和response
- 中间件必须要调用next()
- 避免代码逻辑混乱,不要在next()后加入其他代码