var http = require("http"); function process_request(req,res) { var body = 'Thanks for calling!\n'; var content_length = body.length; res.writeHead(200,{ 'Content-Length':content_length, 'Content-Type':'text/plain' }); res.end(body); } var s = http.createServer(process_request); s.listen(8080)
http模块容许建立服务器,能够食用require来引入模块
createServer 函数只会接受一个函数参数, 它会在用户链接到服务器时被调用。node
屡次执行node index.js可能会报错,缘由是在你监听端口时这个线程会一直处于监听状态。npm
一句命令,中止某个端口
1.全局安装服务器
npm install -g xl_close_port
2.关闭某一个端口 ( 8081 )函数
xl_close_port -p 8081