如何在ExpressJS中以条件状态渲染页面?

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

How can I render pages in conditional states in ExpressJS?

问题

以下是您提供的代码块的翻译部分:

var express = require('express');

var newsRouter = express.Router();

newsRouter.get('/:news_param', (req, res) => {
    let news_params = '/haberler/' + req.params.news_param;

    req.news_list.forEach((news_obj) => {
        if (news_params == news_obj.news_addr) res.render(req.params.news_param);
    });
});

newsRouter.post('/', (req, res) => {
    res.render('news', { news_fetch_from_database: news_list });
})

newsRouter.use((req, res) => {
    res.render('404_page_news');
});

module.exports = newsRouter;

newsRouter.get('/:news_param', ...); 当我像这样添加 res.redirect('/404'); 并访问与404页面不同的任何页面时,会产生以下错误:

...

newsRouter.get('/:news_param', (req, res) => {
    let news_params = '/haberler/' + req.params.news_param;

    req.news_list.forEach((news_obj) => {
        if (news_params == news_obj.news_addr) res.render(req.params.news_param);
    });

    res.redirect('/404');
...
});
Error [ERR_HTTP_HEADERS_SENT]: 在响应头已发送给客户端后无法设置头
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:767:10)
    at ServerResponse.location (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:884:15)
    at ServerResponse.redirect (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:922:18)
    at /home/alimkoca/Softwares/szal_web/news.js:12:9
    at Layer.handle [as handle_request] (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/layer.js:95:5)

我需要轻松处理 req.params 中的参数以便渲染页面,但我现在无法解决这个问题。并且出于安全问题,我不想使用文件系统API。

我只是运行了 npm start 命令并访问了任何有效的页面,然后像我上面表达的那样产生了以下错误:

Error [ERR_HTTP_HEADERS_SENT]: 在响应头已发送给客户端后无法设置头
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:767:10)
    at ServerResponse.location (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:884:15)
    at ServerResponse.redirect (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:922:18)
    at /home/alimkoca/Softwares/szal_web/news.js:12:9
    at Layer.handle [as handle_request] (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/layer.js:95:5)
英文:

I have the following code block:

var express = require('express');

var newsRouter = express.Router();

newsRouter.get('/:news_param', (req, res) => {
    let news_params = '/haberler/' + req.params.news_param;

    req.news_list.forEach((news_obj) => {
        if (news_params == news_obj.news_addr) res.render(req.params.news_param);
    });
});

newsRouter.post('/', (req, res) => {
    res.render('news', { news_fetch_from_database: news_list });
})

newsRouter.use((req, res) => {
    res.render('404_page_news');
});

module.exports = newsRouter;

newsRouter.get('/:news_param', ...); gives the following error when I add res.redirect('/404'); like this and visit any page different than 404 page:

...

newsRouter.get('/:news_param', (req, res) => {
    let news_params = '/haberler/' + req.params.news_param;

    req.news_list.forEach((news_obj) => {
        if (news_params == news_obj.news_addr) res.render(req.params.news_param);
    });

    res.redirect('/404');
...
});
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:767:10)
    at ServerResponse.location (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:884:15)
    at ServerResponse.redirect (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:922:18)
    at /home/alimkoca/Softwares/szal_web/news.js:12:9
    at Layer.handle [as handle_request] (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/layer.js:95:5)

And I need to handle parameters in req.params for rendering pages easily, but I can't solve this issue right now. And I don't want to use filesystem API for security issues.

I simply ran the npm start command and visited any valid page, then it gave the following error as I expressed above:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at new NodeError (node:internal/errors:387:5)
    at ServerResponse.setHeader (node:_http_outgoing:644:11)
    at ServerResponse.header (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:767:10)
    at ServerResponse.location (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:884:15)
    at ServerResponse.redirect (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/response.js:922:18)
    at /home/alimkoca/Softwares/szal_web/news.js:12:9
    at Layer.handle [as handle_request] (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/alimkoca/Softwares/szal_web/node_modules/express/lib/router/layer.js:95:5)

答案1

得分: 1

The code is always, unconditionally performing this operation:

这段代码始终无条件执行此操作:

res.redirect('/404');

Which will fail with the error given if the code has already performed this operation:

如果代码已经执行了此操作,则会失败,并显示给定的错误:

res.render(req.params.news_param);

For exactly the reason implied by the error... You can't return a response after you've already returned a response.

这个错误的原因正如错误所暗示的那样...在已经返回响应之后,你不能再返回响应。

It sounds like you want to only conditionally return the 404 response. For example, if no matching record was found:

看起来你只想在某些条件下返回404响应。例如,如果没有找到匹配的记录:

let news_params = '/haberler/' + req.params.news_param;
let record_found = false;

req.news_list.forEach((news_obj) => {
if (news_params == news_obj.news_addr) {
res.render(req.params.news_param);
record_found = true;
}
});

if (!record_found) {
res.redirect('/404');
}

Or even just return from the function when the record is found:

或者当找到记录时,甚至可以从函数中返回:

let news_params = '/haberler/' + req.params.news_param;

for (let news_obj of req.news_list) {
if (news_params == news_obj.news_addr) {
res.render(req.params.news_param);
return;
}
}

res.redirect('/404');

英文:

The code is always, unconditionally performing this operation:

res.redirect('/404');

Which will fail with the error given if the code has already performed this operation:

res.render(req.params.news_param);

For exactly the reason implied by the error... You can't return a response after you've already returned a response.

It sounds like you want to only conditionally return the 404 response. For example, if no matching record was found:

let news_params = '/haberler/' + req.params.news_param;
let record_found = false;

req.news_list.forEach((news_obj) => {
  if (news_params == news_obj.news_addr) {
    res.render(req.params.news_param);
    record_found = true;
  }
});

if (!record_found) {
  res.redirect('/404');
}

Or even just return from the function when the record is found:

let news_params = '/haberler/' + req.params.news_param;

for (let news_obj of req.news_list) {
  if (news_params == news_obj.news_addr) {
    res.render(req.params.news_param);
    return;
  }
}

res.redirect('/404');

huangapple
  • 本文由 发表于 2023年6月13日 00:55:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458790.html
匿名

发表评论

匿名网友

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

确定