原文:http://blog.csdn.net/xn_28/article/details/50837019
const http = require('http');
const hostname = '127.0.0.1';
const port = 1337;
http.createServer((req, res) => {
res.setHeader("Access-Control-Allow-Origin", "*");
res.writeHead(200, { "Content-Type": "application/json" });
var otherArray = ["item1", "item2"];
var otherObject = { item1: "item1val", item2: "item2val" };
var json = JSON.stringify({
anObject: otherObject,
anArray: otherArray
});
res.end(json)//!!一定要加配置的回调方法名
// res.writeHead(200, { 'Content-Type': 'text/plain' });
// res.end(addon.hello());
}).listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});