用node的express框架写一个接口服务

let express = require('express');
let app =express();
let bodyParser = require('body-parser');


//设置跨域访问
app.all('*', function (req, res, next) {
  res.header('Access-Control-Allow-Origin', 'http://test.test.com:8080');
  res.header('Access-Control-Allow-Credentials', true);
  res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
  res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
  res.header('Content-Type', 'application/json;charset=utf-8');
  next()
});

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());  //data参数以字典格式传输

app.post('/register',(req, res)=>{
    console.log(req.body);   // 打印一个对象 ,例如:{name:'zs',age:'12'}
    res.send(req.body);    // 不能发送数字,只能发字符串
 });


app.get('/api/user', (req, res) => {
    res.send("get请求正常");
});

//配置服务端口
var server = app.listen(8000, () => {

    console.log("node接口服务正常运行");

});

需要安装node,执行npm install express -save、npm install body-parser -D

猜你喜欢

转载自www.cnblogs.com/qiaoer1993/p/13366016.html