nodejs使用express模块使用过滤器添加跨越访问控制
1. npm install express 安装express模块
2. 使用express模块添加filter
const express = require('express');
const app = express();
let filter = (req, res, next) => {
res.setHeader("Access-Control-Allow-Origin", "*"); //添加跨越访问控制
next();
}
app.use(filter);
//需要npm install body-parser 安装该模块
const bodyParser = require('body-parser');
// application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// application/json
app.use(bodyParser.json());
app.post('*', (req, res) => {
var params = req.body //body-parser模块提供的body获取post请求体内容
console.log(params)
res.send('post ok')
})
app.listen(9090, function () { //监听9090端口
console.log('server start');
});