Cannot set headers after they are sent to the client at new NodeError

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

Cannot set headers after they are sent to the client at new NodeError

问题

我尝试创建一个小的重载动画作为中间件,位于请求和响应之间,所以当用户等待响应时,浏览器中会运行一个重载等待动画(我正在使用Node.js和Express,以及EJS模板引擎)。

这是app.js代码:

const ejs = require('ejs');
const express = require('express');
const path = require('path');
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use((req, res, next) => {
    res.render('base', { child: 'middleware' });
    next();
});
app.get('/', (req, res) => {
  res.render('base', { child: 'child' });
});
app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

这是base.ejs:

<html>
  <head>
    <title>Parent Template</title>
  </head>
  <body>
    <header>
      <!-- Common header content -->
    </header>
    <main>
      <!-- Content specific to child files will go here -->
      <%- include(child) %>
    </main>
    <footer>
      <!-- Common footer content -->
    </footer>
  </body>
</html>

两个子文件扩展自base.ejs:

<link rel="stylesheet" href="../static/middleware.css" />
<section>
  <div class="site_title"></div>
</section>

<section>
  <h1>Welcome to the Child Page!</h1>
  <!-- Additional content specific to the child file -->
</section>

我按照上面的代码编写了这个代码。
base.ejs是父模板,用于包含home.ejs和middleware_animation.ejs。对于Node.js模板引擎来说,有点混乱。你能否推荐其他更容易上手的引擎?

英文:

iam tryna make a lil reload animation as middleware between the req and res so when the user waits for a response, a reload waiting runs in browser (im using nodejs & express, and ejs templating engine)

this is the app.js code

const ejs = require(&#39;ejs&#39;);
const express = require(&#39;express&#39;);
const path = require(&#39;path&#39;);
const app = express();
app.set(&#39;views&#39;, path.join(__dirname, &#39;views&#39;));
app.set(&#39;view engine&#39;, &#39;ejs&#39;);
app.use((req, res, next) =&gt; {
    res.render(&#39;base&#39;, { child: &#39;middleware&#39; });
    next();
});
app.get(&#39;/&#39;, (req, res) =&gt; {
  res.render(&#39;base&#39;, { child: &#39;child&#39; });
});
app.listen(3000, () =&gt; {
  console.log(&#39;Server is running on port 3000&#39;);
});

this is base.ejs

&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;Parent Template&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;header&gt;
      &lt;!-- Common header content --&gt;
    &lt;/header&gt;
    &lt;main&gt;
      &lt;!-- Content specific to child files will go here --&gt;
      &lt;%-include (child);-%&gt;
    &lt;/main&gt;
    &lt;footer&gt;
      &lt;!-- Common footer content --&gt;
    &lt;/footer&gt;
  &lt;/body&gt;
&lt;/html&gt;

the two child files to extend from base.ejs

&lt;link rel=&quot;stylesheet&quot; href=&quot;../static/middleware.css&quot; /&gt;
&lt;section&gt;
  &lt;div class=&quot;site_title&quot;&gt;&lt;/div&gt;
&lt;/section&gt;

and

&lt;section&gt;
  &lt;h1&gt;Welcome to the Child Page!&lt;/h1&gt;
  &lt;!-- Additional content specific to the child file --&gt;
&lt;/section&gt;

I wrote this code as clear above
base.ejs, the parent to include home.ejs and middleware_animation.ejs
its kinda messy for the nodejs templatning engine, would you please recommend other easier engines to start with?

答案1

得分: 1

删除此规则或指定一个路由:

app.use((req, res, next) => {
    res.render('base', { child: 'middleware' });
    next();
});

我认为错误消息是由这个规则引起的。使用 res 时已经发送了响应,所以在这里使用 next 是多余的。总的来说,这个路由没有意义,因为没有指定任何路径,所有请求都会进入这里。

英文:

Delete this rule or specify a route:

app.use((req, res, next) =&gt; {
    res.render(&#39;base&#39;, { child: &#39;middleware&#39; });
    next();
});

I think the error message comes from this rule. With res a response already goes out. So next is too much here. In general, this route makes no sense, since no path is specified and all requests go in here.

huangapple
  • 本文由 发表于 2023年6月19日 14:30:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504127.html
匿名

发表评论

匿名网友

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

确定