Node.js实战(三)之第一个Web服务器

这次的示例同样也可以说是HelloWorld,只不过不同的是这是web服务器示例。

(1)编写web.js,内容如下:

var http = require("http")

function process_request(req,res){
   var body = "hello MrYou!\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);
           

(2)运行web.js

node web.js

(3)打开浏览器输入:http://IP:8080/ 回车

通过浏览器调试可以查看对应的详情信息,例如我通过Chrome调试

当然了,还可以通过curl -i http://IP:8080/这样的命令查看对应的信息,例如:

curl -i http://192.168.126.139:8080/

输出结果如下:

猜你喜欢

转载自www.cnblogs.com/youcong/p/10163950.html