"Unhandled Error" 通过节点 js 中的 http 模块设置服务器时的消息
"Unhandled Error" message while setting up server through http module in node js
我一直在编写以下代码来通过 node.js 设置基本服务器。
我的代码-
const http = require('http')
const server = http.createServer((req, res) => {
// console.log(req)
if(req.url === '/'){
res.end("Welcome to our home page")
}
if(req.url === '/aboutus'){
res.end("Welcome to about us page")
}
res.end(`
<h1> OOPS !!! </h1>
<p>You have requested for any wrong address </p>
<a href = "/">Get back to HOME Page</a>
`)
})
server.listen(5000)
但是这段代码 运行 在我的控制台中出现了以下错误。
错误--
node:events:368
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (/home/tirtharaj/MERN/Node/tutorial(fcc)/8-Built-in Modules/6-httpModule(brief).js:10:9)
at Server.emit (node:events:390:28)
at parserOnIncoming (node:_http_server:951:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
我在我的代码中找不到任何错误。网络浏览器中的任何刷新都会出错。所以请帮我 运行 这个代码。
这将解决您的问题,您需要将所有代码包含在 if else 语句中,以便它始终可以成功终止,只有一种可能的代码路径。
const http = require("http");
const server = http.createServer((req, res) => {
if (req.url === "/") {
res.end("Welcome to our home page");
} else if (req.url === "/aboutus") {
res.end("Welcome to about us page");
} else {
res.end(`
<h1> OOPS !!! </h1>
<p>You have requested for any wrong address </p>
<a href = "/">Get back to HOME Page</a>
`);
}
});
server.listen(5000);
就我个人而言,我会为此使用 switch case 语句以保持它的整洁。
const http = require("http");
const server = http.createServer((req, res) => {
switch (req.url) {
case "/":
res.end("Welcome to our home page");
break;
case "/aboutus":
res.end("Welcome to about us page");
break;
default:
res.end(`
<h1> OOPS !!! </h1>
<p>You have requested for any wrong address </p>
<a href = "/">Get back to HOME Page</a>
`);
break;
}
});
server.listen(5000);
我一直在编写以下代码来通过 node.js 设置基本服务器。 我的代码-
const http = require('http')
const server = http.createServer((req, res) => {
// console.log(req)
if(req.url === '/'){
res.end("Welcome to our home page")
}
if(req.url === '/aboutus'){
res.end("Welcome to about us page")
}
res.end(`
<h1> OOPS !!! </h1>
<p>You have requested for any wrong address </p>
<a href = "/">Get back to HOME Page</a>
`)
})
server.listen(5000)
但是这段代码 运行 在我的控制台中出现了以下错误。 错误--
node:events:368
throw er; // Unhandled 'error' event
^
Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:371:5)
at ServerResponse.end (node:_http_outgoing:846:15)
at Server.<anonymous> (/home/tirtharaj/MERN/Node/tutorial(fcc)/8-Built-in Modules/6-httpModule(brief).js:10:9)
at Server.emit (node:events:390:28)
at parserOnIncoming (node:_http_server:951:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:726:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
我在我的代码中找不到任何错误。网络浏览器中的任何刷新都会出错。所以请帮我 运行 这个代码。
这将解决您的问题,您需要将所有代码包含在 if else 语句中,以便它始终可以成功终止,只有一种可能的代码路径。
const http = require("http");
const server = http.createServer((req, res) => {
if (req.url === "/") {
res.end("Welcome to our home page");
} else if (req.url === "/aboutus") {
res.end("Welcome to about us page");
} else {
res.end(`
<h1> OOPS !!! </h1>
<p>You have requested for any wrong address </p>
<a href = "/">Get back to HOME Page</a>
`);
}
});
server.listen(5000);
就我个人而言,我会为此使用 switch case 语句以保持它的整洁。
const http = require("http");
const server = http.createServer((req, res) => {
switch (req.url) {
case "/":
res.end("Welcome to our home page");
break;
case "/aboutus":
res.end("Welcome to about us page");
break;
default:
res.end(`
<h1> OOPS !!! </h1>
<p>You have requested for any wrong address </p>
<a href = "/">Get back to HOME Page</a>
`);
break;
}
});
server.listen(5000);