node解决跨域
代码
// 导入http模块:
var http = require('http');
// 导入url模块 提供三个方法url.parse(); url.format(); url.resolve()
const url = require("url");
// 创建http server,并传入回调函数:
var server = http.createServer(function (request, response) {
----------------重要----------------------
/**
* 解决跨域问题
*/
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");
res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
// END
---------------------------------------------
// 将HTTP响应的HTML内容写入response:
response.end({code:1000});
});
// 让服务器监听8080端口:
server.listen(8080);