express—处理请求
1. 搭建一个app应用
var express = require('express');
var app = express();
2. 请求处理响应
- GET请求
/*path:只需一级路径*/
app.get(path,(req,res) => {
/*获取请求参数,获取到的是一个对象*/
console.log(req.query);
/*响应:无需html响应头,默认设置text/html*/
res.send('get响应的数据');
})
- POST请求
- 下载中间件body-parser
- 导入body-parser中间件
- body-parser中间件绑定到 app 对象
- 才能获取到请求参数
app.post(path,(req,res) => {
/*依赖 body-parser中间件才有body属性,获取参数,得到的是一个对象*/
console.log(req.body);
/*响应:无需html响应头,默认设置text/html*/
res.send('post响应的数据');
})
3. 开启服务
app.listen(3000,[IP],(err) =>{
console.log('server start');
})