英文:
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('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');
});
this is 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>
the two child files to extend from base.ejs
<link rel="stylesheet" href="../static/middleware.css" />
<section>
<div class="site_title"></div>
</section>
and
<section>
<h1>Welcome to the Child Page!</h1>
<!-- Additional content specific to the child file -->
</section>
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) => {
res.render('base', { child: 'middleware' });
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论