搭建一个最简单的node服务器
1、创建一个Http服务并监听8888端口
2、使用url模块 获取请求的路由和请求参数
var http = require(‘http‘); var url = require(‘url‘); http.createServer(function (req, res) { var pathname = url.parse(req.url).pathname; //解析路由请求地址 var params = url.parse(req.url, true).query; //解析请求参数 console.log(req.method+" Request for " + pathname + " received."); //打印请求 res.writeHead(200, { ‘Content-Type‘: ‘text/plain‘ }); res.write(req.method); res.write(‘\npathname:‘+pathname); res.write(‘\nparams:‘+JSON.stringify(params)); res.end(); }).listen(8888); console.log(‘Server running at http://127.0.0.1:8888/‘);