3. http.server类
- request事件,每次接收到一个请求时触发
- Close事件,当服务器关闭时触发
- Server.listen()开启HTTP服务器监听连接
- Server.close()停止服务端接收的新连接
const http = require('http');
// 创建服务
const server = http.createServer();
//server事件
server.on('request',function (req,res) {
res.writeHeader(200,{
'Content-type':'text/html;charset=utf8'
});
res.end('服务器开启');
server.close();
});
server.on('close',function () {
console.log('服务器关闭');
});
server.listen(8086,function () {
console.log('http server is running on port 8086');
});