maomingchao

简单的node 服务端 响应get,返回json数据;

原文: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}/`);
});  

posted on 2017-02-23 14:47  maomingchao  阅读(517)  评论(0)    收藏  举报

导航