node,powershell报错,浏览器标签里的icon图标显示两秒后消失?

网站服务器已成功启动:

img

sublime text代码编辑器界面里有两个文件,一个是app.js,另一个是form.html:

img

其中,app.js里的代码如下:

const http = require('http');
const app = http.createServer();
app.on('request', (req, res) => {
    // console.log(req.method);
    console.log(req.url);
    if (req.url == '/index' || req.url == '/') {
        res.end('welcome to homepage');
    } else if (req.url == '/list') {
        res.end('welcome to listpage');
    } else {
        res.end('not found');
    }
    
    if (req.method == 'GET') {
        res.end('get')
    } else if (req.method == 'POST') {
        res.end('post')
    }
    // res.end('

hello user

'
); }); app.listen(3000); console.log('网站服务器启动成功');

form.html里的代码如下:

html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Documenttitle>
head>
<body>
    <form method="post" action="http://localhost:3000">
        <input type="submit" name="">
    form>
body>
html>

在sublime text代码编辑器里的form.html处右键选择open in browser,浏览器弹出界面后点击提交按钮,显示如下,浏览器标签显示出react的icon图标:

img

然而,不过两秒钟的时间,该react的icon图标消失,显示如下:

img

同时,powershell报错:

img

**node:events:491
throw er; // Unhandled 'error' event
^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
at new NodeError (node:internal/errors:387:5)
at ServerResponse.end (node:_http_outgoing:925:15)
at Server. (D:\Node.js\3-code\server\app.js:17:7)
at Server.emit (node:events:513:28)
at parserOnIncoming (node:_http_server:980:12)
at HTTPParser.parserOnHeadersComplete (node:_http_common:128:17)
Emitted 'error' event on ServerResponse instance at:
at emitErrorNt (node:_http_outgoing:786:9)
at processTicksAndRejections (node:internal/process/task_queues:84:21) {
code: 'ERR_STREAM_WRITE_AFTER_END'
}
[nodemon] app crashed - waiting for file changes before starting...**

搜索过网上很多关于node491错误的解决方法,但都不能解决问题。
请问是什么原因导致报错?如何解决?恳请展示解决此问题的每一个步骤,谢谢

注意 :res.end 代表了输出结束了,你后面又有一个判断,调用了res.end,必然出错了。你可以在前面的判断使用res.write,或者注释后面的判断,就不会出错了。

const http = require('http');
const app = http.createServer();
app.on('request', (req, res) => {
    // console.log(req.method);
    console.log(req.url);
    if (req.url == '/index' || req.url == '/') {
        res.write('welcome to homepage');
    } else if (req.url == '/list') {
        res.write('welcome to listpage');
    } else {
        res.write('not found');
    }
    // 注释下面,或者上面用write,这里用end
    if (req.method == 'GET') {
        res.end('get')
    } else if (req.method == 'POST') {
        res.end('post')
    }

});
app.listen(3000);
console.log('网站服务器启动成功');