const http = require('http'); const app = http.createServer(); const url = require('url'); const path = require('path'); const fs = require('fs'); const mime = require('mime'); app.on('request', (req, res) => { // 处理请求 let pathname = url.parse(req.url).pathname; pathname = pathname == '/' ? '/default.html' : pathname; let realPath = path.join(__dirname, 'public' + pathname); let type = mime.getType(realPath); fs.readFile(realPath, (error, result) => { if (error != null) { res.writeHead(404, { 'content-type': 'text/html;charset=utf8' }); res.end('文件读取失败'); } else { res.writeHead(200, { 'content-type': type }); res.end(result); }; }); // 监听 3000 端口 app.listen(3000); console.log('服务器已启动,监听 3000 端口,请访问 localhost:3000');检查这段代码的错误
时间: 2024-03-27 09:41:16 浏览: 61
这段代码可能存在一个错误,即在读取文件成功时的 else 分支中,应该在最后加上一个分号。正确的代码如下:
```
const http = require('http');
const app = http.createServer();
const url = require('url');
const path = require('path');
const fs = require('fs');
const mime = require('mime');
app.on('request', (req, res) => {
// 处理请求
let pathname = url.parse(req.url).pathname;
pathname = pathname == '/' ? '/default.html' : pathname;
let realPath = path.join(__dirname, 'public' + pathname);
let type = mime.getType(realPath);
fs.readFile(realPath, (error, result) => {
if (error != null) {
res.writeHead(404, { 'content-type': 'text/html;charset=utf8' });
res.end('文件读取失败');
} else {
res.writeHead(200, {
'content-type': type
});
res.end(result);
}
});
});
// 监听 3000 端口
app.listen(3000);
console.log('服务器已启动,监听 3000 端口,请访问 localhost:3000');
```
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
const fs = require("fs"); const http = require("http"); const {promisify} = require("util"); const path = require("path"); let server = http.createServer(async (req,res)=>{ let urlObj = new URL(req.url,"http://"+req.headers.host); let removeDir = require("./removedir") let {pathname}=urlObj; res.setHeader("Content-Type","text/html;charset=utf-8") if(pathname ==="/"){ let pathurl = "./";/* 当前目录 */ let showurl = urlObj.searchParams.get("showurl"); if(showurl){ pathurl = showurl; } /* 判断是否为文件 */ let stat = fs.statSync(pathurl); if (stat.isFile()) { // 是文件 let filedata = await promisify(fs.readFile)(pathurl); res.write(filedata); res.end(); } // 判断是否需要执行删除的逻辑 let delpath = urlObj.searchParams.get("delpath"); if (delpath) { // 有 delpath参数 ,那么证明需要删除逻辑; if (fs.existsSync(delpath)) { let stat = fs.statSync(delpath); if (stat.isFile()) { // 是文件 : fs.unlinkSync(delpath); } else { // 是目录 removeDir(delpath); } } } let resdata = await promisify(fs.readdir)(pathurl); let tablestr = "文件名 创建时间 操作 "; resdata.forEach(item=>{ let myurl = path.join(pathurl,item); let stat = fs.statSync(myurl); console.log(stat); tablestr += ${item} ${stat.birthtime} 删除 }) //console.log(tablestr,"????????????????????????????") tablestr += ""; res.write(tablestr); res.end(); } } ) server.listen(8888);
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)