//引入http模块
const http = require('http');
// console.log(http);
//创建web服务器:
const server = http.createServer();
// console.log(server);
//监听事件:server.on('事件名',回调方法);
//req对象:request请求对象
//res对象:response响应对象
server.on('request', function(req, res) {
console.log(req.url);
//res.setHeader()或res.writeHead():设置响应的头信息
// res.setHeader('content-type', 'text/html;charset=utf-8');
res.writeHead(200, 'ok', { 'content-type': 'text/html;charset=utf-8' });
res.write('<h2>张三</h2>'); //返回响应的内容、res.write()可以多次使用
res.write('<p><strong>年龄:20</strong></p>');
res.end('Yes OK');
// res.end('Hello web'); //结束响应、res.end()必须且只能使用一次
});
//监听端口:
server.listen(3000, function() {
console.log('3000端口');
});