Ken428965 2023-01-20 18:56 采纳率: 53.9%
浏览 17
已结题

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里的代码如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</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错误的解决方法,但都不能解决问题。
请问是什么原因导致报错?如何解决?恳请展示解决此问题的每一个步骤,谢谢

  • 写回答

1条回答 默认 最新

  • 桑榆肖物 全栈领域新星创作者 2023-01-20 22:18
    关注

    注意 :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('网站服务器启动成功');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月22日
  • 已采纳回答 1月21日
  • 创建了问题 1月20日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改