Node.js快速搭建服务器

来源:互联网 发布:英文写作检查软件 编辑:程序博客网 时间:2024/06/14 07:49

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

原创粉丝点击