使用http模块构建一个服务器

代码如下:

const http = require('http');
/*
	request 获取url传来的信息
	response 给浏览器的响应信息
*/ 
http.createServer(function (request, response) {
  // 设置响应头
  response.writeHead(200, {'Content-Type': 'text/plain'});
  // 表明响应头已发送完成,不可缺
  response.end('Hello World');
    
  // 也可以采用链式写法
  response.writeHead().end()
}).listen(8081);

console.log('Server running at http://127.0.0.1:8081/');
posted @ 2021-02-19 19:36  ychizzz  阅读(111)  评论(0)    收藏  举报