ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
~~~ //根据用户不同请求做出不同的响应 //加载http模块 var http=require('http'); //加载fs模块 var fs=require('fs'); //加载path模块 var path=require('path'); //创建http服务,并启动该服务 http.createServer(function(req,res){ //通过req.url获取用户请求的路径,根据不同的请求路径服务器做出不同的响应 if(req.url==='/'||req.url==='/index'){ fs.readFile(path.join(__dirname,'htmls','index.html'),function(err,data){ if(err){ throw err; } //把读取到的index.html中的内容直接发送给浏览器 res.end(data); }) }else if(req.url==='/login'){ fs.readFile(path.join(__dirname,'htmls','login.html'),function(err,data){ if(err){ throw err; } //把读取到的index.html中的内容直接发送给浏览器 res.end(data); }) }else if(req.url==='/list'){ }else if(req.url==='/register'){ }else{ fs.readFile(path.join(__dirname,'htmls','404.html'),function(err,data){ if(err){ throw err; } //把读取到的index.html中的内容直接发送给浏览器 res.end(data); }) } }).listen(8080,function(){ console.log("http://localhost:8080"); }); ~~~ ![](https://box.kancloud.cn/d2c6c606f115f1d263e27494c435b38d_133x90.png) ~~~ //index.html中的内容 <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>hello index</title> </head> <body> <h1 style="color: red;">hello world</h1> <h1>你好首页</h1> </body> </html> ~~~