NodeJS搭建简易服务器

// http.js
//引入内置http模块
var http = require("http")
// 创建服务器
var server = http.createServer(function(req,res){
	// //设置响应状态码,响应头(编码格式)
    res.writeHead(200, {"Content-Type" : "text/plain; charset=utf-8"});
   //  //设置响应内容
    res.write("hello node.js!");
   //  //结束响应
    res.end()
})

// 设置服务器端口
server.listen(3000)

运行服务器

在CMD命令窗口中切换到该服务器文件所在目录,然后 输入node http.js,回车!

浏览器打开测试

打开浏览器,在地址栏输入localhost:3000,回车!
image.png

原文详解:https://www.cnblogs.com/saber200/p/5242732.html

posted @ 2019-08-11 22:35  Silence_1018  阅读(6)  评论(0)    收藏  举报  来源