//创建网站服务器模板
const http = require('http');
//网站服务器对象
const app = http.createServer();
//客户端有请求
app.on('request', (req, res) => {
//获取请求方式
//console.log(req.method);
//获取请求地址
//console.log(req.url);
//响应报文
res.writeHead(200, {
'content-type': 'text/html;charset=utf8'
});
if (req.url == '/index' || req.url == '/') {
res.end('<h2>welcome to index</h2>')
} else if (req.url == '/list') {
res.end('welcome to list')
} else {
res.end('not found');
}
if (req.method == 'POST') {
res.end('post')
} else if (req.method == 'get') {
res.end('get')
}
//res.end('<h2>hello user</h2>')
});
//监听端口
app.listen(3000);
console.log('服务器启动成功');
运行结果