// 模拟apache访问指定目录下的资源 var wwwPath = 'D:/Tomcat/apache-tomcat-10.0.5-windows-x64/apache-tomcat-10.0.5/webapps' var http = require('http'); var fs = require('fs'); var server = http.createServer(); server.on('request',function(req,res){ var url = req.url; var filePath = '/index.html'; if(url !== '/'){ filePath = url; } fs.readFile(wwwPath+filePath, function(err,data){ if(err){ return res.end('404 Not Found.'); } res.end(data); }) }); server.listen(3000,function(){ console.log('服务器启动成功,请通过localhost:3000 进行访问'); })