// 1. 引入系统模块http
const http = require('http');
const url = require('url');
// 2 .创建网站服务器
const app = http.createServer();
// 3. 为网络服务器对象添加请求事件
// 4. 实现路由功能
//(1)获取客户端的请求方式
//(1)获取客户端的请求地址
app.on('request', (req, res) => {
// 获取请求方式
const method = req.method.toLowerCase(); // 获取GET或POST并转换为小写
// 获取请求地址
const pathname = url.parse(req.url.pathname);
res.writeHead(200, {
'content-type': 'text/html;charset = utf8'
});
if (method == 'get') {
if (pathname == '/' || pathname == '/index') {
res.end('欢迎来到首页');
} else if (pathname == '/list') {
res.end('欢迎来到列表页');
} else {
res.end('您访问的页面不存在');
}
} else if (method == 'post') {
}
});
app.listen(3000);
console.log('服务器启动成功!');
提示很明显了。
缺少运行参数。
你打印下req.url看看就知道了,他没有pathname属性