简体   繁体   English

节点js Express错误:ERR_TOO_MANY_REDIRECTS

[英]Node js Express Error: ERR_TOO_MANY_REDIRECTS

Everything worked just fine until all of a sudden this error occurred: ERR_TOO_MANY_REDIRECTS. 一切正常,直到突然发生此错误:ERR_TOO_MANY_REDIRECTS。 I had no luck in figuring it out on my own so please help me if you can. 我靠自己搞定没有运气,所以如果可以的话请帮助我。

I use express-handlebars and express to route my pages and I thing I saw where the problem occurres. 我使用express-handlebars和express路由页面,我发现问题出在哪里。 In my bookController file when I use res. 当我使用res时,在我的bookController文件中。 render ('something') no matter from what route it always redirects me to '/' and when I use res. 渲染 (“某物”),无论它从哪条路线总是将我重定向到“ /”以及何时使用res。 send ('something') It redirects to where it defined. 发送 (“某物”)它重定向到它定义的位置。

This is my app.js file: 这是我的app.js文件:

 const express = require("express"); const mongoose = require("mongoose"); const passport = require("passport"); const path = require("path"); const bodyParser = require("body-parser"); const hbs = require("express-handlebars"); const cookieParser = require("cookie-parser"); const session = require("express-session"); const MongoStore = require("connect-mongo")(session); const flash = require("connect-flash"); const routes = require("./routes/index"); const expressValidator = require("express-validator"); const errorHandlers = require("./handlers/errorHandlers"); const app = express(); const webpack = require("webpack"); const webpackConfig = require("./webpack.config.js"); const compiler = webpack(webpackConfig); const webpackDevMiddleware = require("webpack-dev-middleware")( compiler, webpackConfig.devServer ); app.use(webpackDevMiddleware); app.use(require("webpack-hot-middleware")(compiler)); app.use(express.static(path.join(__dirname, "public"))); app.engine( "hbs", hbs({ extname: "hbs", defaultLayout: "main", layoutsDir: __dirname + "/views/layouts", partialsDir: __dirname + "/views/partials" }) ); app.set("views", path.join(__dirname, "views")); app.set("view engine", "hbs"); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(flash()); app.use( session({ resave: false, saveUninitialized: false, secret: process.env.SECRET }) ); app.use(cookieParser()); app.use(expressValidator()); app.use((req, res, next) => { res.locals.flashes = req.flash(); next(); }); app.use("/", routes); app.use(errorHandlers.flashValidationErrors); module.exports = app; 

This is my index.js file 这是我的index.js文件

 const router = require("express").Router(); const bookController = require("../controllers/bookController"); const { catchErrors } = require("../handlers/errorHandlers"); router.get("/", bookController.home); router.post("/contact", catchErrors(bookController.contactForm)); router.post("/productPage", catchErrors(bookController.productForm)); module.exports = router; 

This is my bookController file: 这是我的bookController文件:

 const mail = require("../handlers/mail"); exports.home = async (req, res) => { res.render("index"); }; exports.contactForm = async (req, res) => { const email = req.body.email; const subject = req.body.subject; const text = req.body.text; await mail.send({ email, subject, text }); res.redirect("/"); }; exports.productForm = async (req, res) => { console.log(req.body); res.redirect("/"); }; 

I will very appreciate any help. 我将非常感谢您的帮助。 Thank you. 谢谢。

Answer which solved the question. 解决了问题的答案。

If res.render() does not stop the request (Same way as res.send(html) ), usually means there is error in the render function. 如果res.render()没有停止请求(与res.send(html) ),通常意味着render函数出错。 As the behaviour of res.render() is that if it errors, it will trigger next(err) . 由于res.render()的行为是如果出错,则将触发next(err)

https://expressjs.com/en/api.html#res.render https://expressjs.com/en/api.html#res.render

When an error occurs, the method invokes next(err) internally. 发生错误时,该方法在内部调用next(err)。

Errors are quite simple to be tracked in the function, by passing an callback into the render; 通过将回调传递到渲染器中,可以很容易地在函数中跟踪错误。

res.render('index', function(err, html) {
  if (err) console.error(err);
  res.send(html);
});

Once you can trace the error and fix it, problem should be solved. 一旦您可以跟踪错误并解决问题,就应该解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Express.js ERR_TOO_MANY_REDIRECTS错误 - Express.js ERR_TOO_MANY_REDIRECTS error 从反应发送请求时 Node.js 错误(net::ERR_TOO_MANY_REDIRECTS) - Node.js error when sending request from react (net::ERR_TOO_MANY_REDIRECTS ) Kubernetes - 'ERR_TOO_MANY_REDIRECTS' 错误 - Kubernetes - 'ERR_TOO_MANY_REDIRECTS' error 此页面无法正常工作 localhost 将您重定向了太多次。 ERR_TOO_MANY_REDIRECTS Express js MongoDB - This page isn’t working localhost redirected you too many times. ERR_TOO_MANY_REDIRECTS Express js MongoDB 响应Azure AD v2.0身份验证的ERR_TOO_MANY_REDIRECTS错误 - ERR_TOO_MANY_REDIRECTS error in response of Azure AD v2.0 authentication 调用 res.redirect 后出现 ERR_TOO_MANY_REDIRECTS - ERR_TOO_MANY_REDIRECTS after calling res.redirect EC2 Ubuntu NGINX 配置 HTTPS 重定向给我错误:ERR_TOO_MANY_REDIRECTS - EC2 Ubuntu NGINX config HTTPS Redirect giving me error: ERR_TOO_MANY_REDIRECTS Nginx 错误重定向太多 - node.js + sockets.ZF98ED07A4D5F50F7DE1410DjsF7F1 + express. - Nginx error too many redirects - node.js + sockets.io + express.js 错误:来自本地主机的TOO_MANY_REDIRECTS在node.js中使用express - Error: TOO_MANY_REDIRECTS from localhost using express in node.js net :: ERR_TOO_MANY_REDIRECTS如果我想在puppeteer中打开网站 - net::ERR_TOO_MANY_REDIRECTS if I want to open website in puppeteer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM