【NodeJS】跨域
转载:https://www.cnblogs.com/yangchongxing/p/10635480.html
var express = require('express'); var app = express(); //设置跨域访问 app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.header("X-Powered-By",' 3.2.1') res.header("Content-Type", "application/json;charset=utf-8"); next(); }); app.get('/api', function (req, res) { var data = { name : 'alice', age : 222 }; res.send(JSON.stringify(data)); }) var server = app.listen(9090, function () { console.log("访问地址为 http://localhost:9090/api"); })