![](/img/trans.png)
[英]Why does my input accept all values, even though I'm using a function to prevent this?
[英]Why does my my nodejs server crash even though I'm handling the supposed error that is occuring?
我正在處理我的第一個 nodejs 服務器示例,它大部分都在工作。 但是,我注意到如果我嘗試調用一個不存在的 html 文件,服務器將會崩潰。 我設法找到了一種解決方法(已被注釋掉),但我很好奇為什么原始代碼未能按預期運行。 在崩潰之前,我會看到所需的響應,然后服務器會崩潰並顯示以下 output:
Server running at http://localhost:3000
Request for /aboutuss.html by method GET
ENOENT: no such file or directory, access '/Users/albertkhasky/NodeJS/node-http/public/aboutuss.html'
events.js:173
throw er; // Unhandled 'error' event
^
Error: ENOENT: no such file or directory, open '/Users/**********/NodeJS/node-http/public/aboutuss.html'
Emitted 'error' event at:
at fs.open (internal/fs/streams.js:117:12)
at FSReqCallback.args [as oncomplete] (fs.js:145:20)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
這是代碼:
const http = require('http');
const fs = require('fs');
const path = require('path');
const hostname = 'localhost'
const port = 3000;
const server = http.createServer((req, res) => {
console.log("Request for " + req.url + ' by method ' + req.method);
if(req.method == 'GET'){
var fileUrl;
if(req.url == '/'){
fileUrl = '/index.html';
}else{
fileUrl = req.url;
}
var filePath = path.resolve('./public' + fileUrl);
const fileExt = path.extname(filePath);
if(fileExt == '.html'){
// if (!fs.existsSync(path)) {
// res.statusCode = 404;
// res.setHeader('Content-Type', 'text/html');
// res.end('<html><body><h1>Error: 404 ' + fileUrl + ' HTML FILE NOT FOUND </h1></body></html>');
// return;
// }
fs.access(filePath, fs.F_OK, (err) => {
if(err){
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
res.end('<html><body><h1>Error: 404 ' + fileUrl + ' HTML FILE NOT FOUND </h1></body></html>');
console.log(err.message);
return;
}
})
}else{
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
res.end('<html><body><h1>Error: 404 ' + fileUrl + ' none HTML file not found</h1></body></html>');
return;
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
fs.createReadStream(filePath).pipe(res);
}else{
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
res.end('<html><body><h1>Error: 404 ' + req.method + ' not supported</h1></body></html>');
return;
}
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}`); //variables inside the string is the reason for backticks
});
您注釋掉的代碼返回 404 的原因是您正在傳遞path
,而不是filePath
所以它每次都失敗。
您的fs.access()
檢查失敗的原因是它是一個異步調用 - 回調檢查err
不會立即發生。 當該檢查正在進行時,代碼無條件地創建一個讀取流到不存在的路徑。 (這也是為什么不推薦使用 fs.existsSync() 調用如果通過 filePath 可以解決問題。)
如果您想等到 fs.access 調用發生,請在檢查錯誤后將成功返回(創建讀取流的位置)移動到 fs.access 回調中:
fs.access(filePath, fs.F_OK, (err) => {
if(err){
res.statusCode = 404;
res.setHeader('Content-Type', 'text/html');
res.end('<html><body><h1>Error: 404 ' + fileUrl + ' HTML FILE NOT FOUND </h1></body></html>');
console.log(err.message);
return;
}
res.statusCode = 200;
res.setHeader('Content-Type', 'text/html');
fs.createReadStream(filePath).pipe(res);
})
執行操作的更有效方法是在文件上使用fs.open()
處理錯誤返回,然后使用fs.createReadStream()
返回的 fd fs.open()
。 這樣您就不會兩次打開文件,也不會出現競爭條件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.