createServer 不显示响应

huangapple go评论65阅读模式
英文:

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)

注意:请检查代码中的以下几个问题,可能是导致您未收到预期响应的原因:

  1. res.writeHead方法中,第二个参数应该是状态消息,而不是字符串。应该是像"OK"这样的状态消息。
  2. res.statusCode方法调用中,不需要括号,应该是res.statusCode而不是res.statusCode()
  3. 确保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)=&gt;{
	console.log(&quot;headerSent:&quot;,res.headersSent())
	console.log(&quot;statusCode :&quot;,res.statusCode ())
	if (req.url == &#39;/&#39;) {
		res.writeHead(200, &quot;succeeded&quot;,{ &#39;Content-Type&#39;: &#39;text/plain&#39; });
		// set response content    
		res.write(&#39;&lt;html&gt;&lt;body&gt;&lt;p&gt;This is default Page.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;&#39;);
		res.end(JSON.stringify({
			data: &#39;default!&#39;,
		  }));
	}

	if (req.url == &#39;/hello&#39;) {
		res.writeHead(200, &quot;succeeded&quot;,{ &#39;Content-Type&#39;: &#39;text/plain&#39; });
		// set response content    
		res.write(&#39;&lt;html&gt;&lt;body&gt;&lt;p&gt;This is hello Page.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;&#39;);
		res.end(JSON.stringify({
			data: &#39;Hello World!&#39;,
		  }));
	}
}
const server = http.createServer(options,functionListener =&gt; {

});
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 =&gt; {

});

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.

huangapple
  • 本文由 发表于 2023年6月22日 15:58:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76529712.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定