英文:
createServer does not show responses
问题
根据下面发布的代码,我正在使用createServer
创建一个服务器。我想要测试它。因此,在浏览器中,我输入了以下网址:
http://localhost:3000/
http://localhost:3000/hello
我期望在第一个if条件
中接收到在前一个URL中指定的文本,因为前一个URL是/
,并且
我期望在第二个if条件
中接收到在后一个URL中指定的文本,因为后一个URL是/hello
但是没有发生上述任何情况。
请您纠正我的理解,并告诉我为什么Web浏览器没有显示任何响应?
代码:
const options = {
keepAlive: true,
}
const functionListener = (req, res) => {
console.log("headerSent:", res.headersSent());
console.log("statusCode:", res.statusCode());
if (req.url == '/') {
res.writeHead(200, "succeeded", { 'Content-Type': 'text/plain' });
// 设置响应内容
res.write('<html><body><p>This is default Page.</p></body></html>');
res.end(JSON.stringify({
data: 'default!',
}));
}
if (req.url == '/hello') {
res.writeHead(200, "succeeded", { 'Content-Type': 'text/plain' });
// 设置响应内容
res.write('<html><body><p>This is hello Page.</p></body></html>');
res.end(JSON.stringify({
data: 'Hello World!',
}));
}
}
const server = http.createServer(options, functionListener => {
});
server.listen(port)
注意:请检查代码中的以下几个问题,可能是导致您未收到预期响应的原因:
- 在
res.writeHead
方法中,第二个参数应该是状态消息,而不是字符串。应该是像"OK"
这样的状态消息。 - 在
res.statusCode
方法调用中,不需要括号,应该是res.statusCode
而不是res.statusCode()
。 - 确保
port
变量已定义,以便server.listen
使用正确的端口。
希望这可以帮助您找到问题并修复您的代码。
英文:
as shown in the code posted below, i am creating a server using createServer
. i would like to test it.therefore, in the browser, i entered the following urls:
http://localhost:3000/
http://localhost:3000/hello
i expected to receive the text stated in the first if-condition
as the former url is /
and
i expected to receive the text stated in the second if-condition
as the latter url is /hello
but none of the aforementioned cases occurred.
would you please recify my understanding and tell me why the web-browser shows no responses?
code:
const options = {
keepAlive:true,
}
const functionListener = (req,res)=>{
console.log("headerSent:",res.headersSent())
console.log("statusCode :",res.statusCode ())
if (req.url == '/') {
res.writeHead(200, "succeeded",{ 'Content-Type': 'text/plain' });
// set response content
res.write('<html><body><p>This is default Page.</p></body></html>');
res.end(JSON.stringify({
data: 'default!',
}));
}
if (req.url == '/hello') {
res.writeHead(200, "succeeded",{ 'Content-Type': 'text/plain' });
// set response content
res.write('<html><body><p>This is hello Page.</p></body></html>');
res.end(JSON.stringify({
data: 'Hello World!',
}));
}
}
const server = http.createServer(options,functionListener => {
});
server.listen(port)
答案1
得分: 2
将这部分代码更改为:
const server = http.createServer(options, functionListener);
您的第一个版本只是将一个空的箭头函数作为参数传递,偶然将其第一个参数命名为functionListener
,与您使用同名定义的函数没有任何关联。
修复后的版本将引用传递给您定义的functionListener
函数,以便http.createServer()
可以按预期调用该函数。
然后,同时发送HTML响应和JSON响应没有意义。选择其中一个,并设置content-type
以匹配。
英文:
To start with change this:
const server = http.createServer(options,functionListener => {
});
to this:
const server = http.createServer(options, functionListener);
Your first version is just passing an empty arrow function as the argument that happens to have its first argument named functionListener
which has no connection at all to the function you defined by that same name.
The fixed version passes a reference to the functionListener
function you defined so that http.createServer()
can call that function as desired.
Then, it doesn't make sense that you're sending BOTH an HTML response and a JSON response. Pick one or the other and set the content-type
to match.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论