1、安装
http://www.runoob.com/nodejs/nodejs-install-setup.html
2、搭建服务器
http://www.runoob.com/nodejs/nodejs-http-server.html
3、返回json数据
3.1、创建json.js文件
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 http://127.0.0.1:8080');
});
3.2、在cmd中执行node json.js
3.3、打开浏览器验证
输入http://127.0.0.1:8080
本文介绍如何使用Node.js快速安装并搭建一个简单的HTTP服务器,该服务器能够返回JSON格式的数据。文章详细展示了创建服务器的步骤,包括编写核心代码、运行服务器及通过浏览器验证返回结果的方法。
2238

被折叠的 条评论
为什么被折叠?



