nodejs输出json

来源:互联网 发布:七彩网络 编辑:程序博客网 时间:2024/05/17 02:39

不依赖任何框架,怎样用原生的nodejs向客户端输出json数据

var http = require('http');



var data = {key: 'value', hello: 'world'};


var srv = http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'application/json'});
  res.end(JSON.stringify(data));
});


srv.listen(8080, function() {
  console.log('listening on localhost:8080');
});
原创粉丝点击