在新的NodeError处将头发送到客户端后,无法设置头

vwhgwdsa  于 2023-06-22  发布在  Node.js
关注(0)|答案(1)|浏览(170)

iam尝试在req和res之间制作一个lil reload动画,这样当用户等待响应时,在浏览器中运行一个reload等待(im使用nodejs & 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的父级,这对nodejs模板引擎来说有点混乱,你能推荐其他更容易的引擎吗?

sg2wtvxw

sg2wtvxw1#

删除此规则或指定路由:

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

我认为错误消息来自此规则。使用res已经发出响应。所以next太多了。一般来说,这个路由没有意义,因为没有指定路径,所有请求都进入这里。

相关问题