nodejs通过http模块创建web服务器

1、导入 http 模块

// 导入 http 模块
const http = require('http');

2、创建 Web 服务器实例

// 创建 Web 服务器实例
const server = http.createServer();

3、为服务器实例绑定 request 事件, 监听客户端的请求

// 为服务器实例绑定 request 事件, 监听客户端的请求
server.on('request', function(req, res) {
  console.log('Someone vist web server');
})

4、启动服务器

// 启动服务器
server.listen(80, function(){
  console.log('Web server is running at http://127.0.0.1');
})

扫描下方二维码,关注公众号:程序进阶之路,实时获取更多优质文章推送。


扫码关注

评论