Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/471.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Nodejs[ERR\u STREAM\u WRITE\u AFTER\u END]:结束后写入_Javascript_Node.js - Fatal编程技术网

Javascript Nodejs[ERR\u STREAM\u WRITE\u AFTER\u END]:结束后写入

Javascript Nodejs[ERR\u STREAM\u WRITE\u AFTER\u END]:结束后写入,javascript,node.js,Javascript,Node.js,我正在学习NodeJS的基础知识。 我正在研究被动网页,我不知道这段代码出了什么问题。当我运行下面的代码时,我遇到了一个错误 var http = require('http'); var fs = require('fs'); var url = require('url'); var app = http.createServer(function(request,response){ var _url = request.url; var queryData = u

我正在学习NodeJS的基础知识。 我正在研究被动网页,我不知道这段代码出了什么问题。当我运行下面的代码时,我遇到了一个错误

var http = require('http');
var fs = require('fs');
var url = require('url');
    var app = http.createServer(function(request,response){
    var _url = request.url;
    var queryData = url.parse(_url, true).query;
    console.log(queryData.id);
    var title = queryData.id;
    if(_url == '/'){
      _url = '/index.html';
      title = 'Welcome';
    }
    if(_url == '/favicon.ico'){
      return response.writeHead(404);
    }
    response.writeHead(200);
    response.end(queryData.id);
    var template = `
    <!doctype html>
    <html>
    <head>
      <title>WEB1 - ${title}</title>
      <meta charset="utf-8">
    </head>
    <body>
      <h1><a href="/">WEB</a></h1>
      <ul>
        <li><a href="/?id=HTML">HTML</a></li>
        <li><a href="/?id=CSS">CSS</a></li>
        <li><a href="/?id=JavaScript">JavaScript</a></li>
      </ul>
      <h2>${title}</h2>
      <p><a href="https://www.w3.org/TR/html5/" target="_blank" title="html5 speicification">Hypertext Markup Language (HTML)</a> is the standard markup language for <strong>creating <u>web</u> pages</strong> and web applications.Web browsers receive HTML documents from a web server or from local storage and render them into multimedia web pages. HTML describes the structure of a web page semantically and originally included cues for the appearance of the document.
      <img src="coding.jpg" width="100%">
      </p><p style="margin-top:45px;">HTML elements are the building blocks of HTML pages. With HTML constructs, images and other objects, such as interactive forms, may be embedded into the rendered page. It provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML elements are delineated by tags, written using angle brackets.
      </p>
    </body>
    </html>
    `;
    response.end(template);
});
app.listen(3000);
var http=require('http');
var fs=需要('fs');
var url=require('url');
var app=http.createServer(函数(请求、响应){
var_url=request.url;
var queryData=url.parse(_-url,true).query;
console.log(queryData.id);
var title=queryData.id;
如果(_url=='/')){
_url='/index.html';
标题=‘欢迎’;
}
如果(_url='/favicon.ico'){
返回响应。写头(404);
}
书面答复(200);
响应.end(queryData.id);
变量模板=`
WEB1-${title}
${title} 是创建网页和web应用程序的标准标记语言。web浏览器从web服务器或本地存储器接收HTML文档,并将其呈现为多媒体网页。HTML从语义上描述网页的结构,最初包括文档外观的提示。

HTML元素是HTML页面的构建块。使用HTML构造,图像和其他对象(如交互表单)可以嵌入到呈现的页面中。它提供了一种通过表示文本的结构语义(如标题、段落、列表、链接、引用和其他项)来创建结构化文档的方法。HTML元素是E由标记创建,使用尖括号书写。

`; 响应。结束(模板); }); app.listen(3000);
这是错误信息

jjunhyeok@jeongjunhyeog-ui-MacBookPro node % node main.js
undefined
events.js:292
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_WRITE_AFTER_END]: write after end
    at writeAfterEnd (_http_outgoing.js:668:15)
    at ServerResponse.end (_http_outgoing.js:789:7)
    at Server.<anonymous> (/Users/jjunhyeok/OneDrive/coding/node/main.js:57:14)
    at Server.emit (events.js:315:20)
    at parserOnIncoming (_http_server.js:874:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:126:17)
Emitted 'error' event on ServerResponse instance at:
    at writeAfterEndNT (_http_outgoing.js:727:7)
    at processTicksAndRejections (internal/process/task_queues.js:81:21) {
  code: 'ERR_STREAM_WRITE_AFTER_END'
jjunhyeok@jeongjunhyeog-ui MacBookPro节点%node main.js
未定义
events.js:292
投掷者;//未处理的“错误”事件
^
错误[错误\u流\u结束后写入]:结束后写入
在writeAfterEnd(_http_outgoing.js:668:15)
在ServerResponse.end(_http_outgoing.js:789:7)
在服务器上。(/Users/jjunhyeok/OneDrive/coding/node/main.js:57:14)
在Server.emit上(events.js:315:20)
在parserOnIncoming(_http_server.js:874:12)
在HTTPParser.parserOnHeadersComplete(_http_common.js:126:17)
在以下位置的ServerResponse实例上发出了“错误”事件:
在writeAfterEndNT(_http_outgoing.js:727:7)
在处理和拒绝时(内部/process/task_queues.js:81:21){
代码:'ERR\u STREAM\u WRITE\u AFTER\u END'

当我运行
节点main.js时,没有错误,但是当我尝试访问网页时,我得到了一个错误。

我尝试过调试,但没有错误。
响应。end
表示发送给客户端的响应结束…要么将徽标
queryData.id
放入模板中,要么发出两个请求。简单地说,您一件事不能结束两次…这不表示senceOh,
queryData.id
不能重复,谢谢。:)