nodeExpress设置请求头.md 673 B

帮郑一做node后端记录

** node express做restful接口时设置统一请求头或设置响应信息

var express = require('express');
var app = express();
// 通配设置统一响应头
app.all('*', async function (req, res,next) {
  //查
  var data= '{}';
  // 设置头部信息
  res.header('Access-Control-Allow-Origin', '*');
  res.header("Content-Type", "application/json;charset=utf-8");
  if(req.originalUrl == '/clubs') {
    data = await clubs()
  }
  ///next()
  res.end(data)
})
var server = app.listen(8089, function () {
  var port = server.address().port
  console.log("应用实例,访问地址为 http://localhost:", port)
})