gpt4 book ai didi

javascript - Express 表单发布请求处理中间件返回 RangeError

转载 作者:行者123 更新时间:2023-11-29 17:36:31 28 4
gpt4 key购买 nike

我有一个表格,我必须从中获取我的数据。我正在使用 ejs 模板。我的“/”路由呈现“主” View ,我有一个中间件来处理来自表单的数据,但它返回错误“RangeError:超出最大调用堆栈大小”

我的 express 代码:

const express = require('express');
const app = express();
const path = require('path');
const bodyParser = require('body-parser');
const validator = require('validator.js');

app.set('view engine','ejs');

const urlEncodedParser = app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());

app.use(express.static(path.join(__dirname + '/public')));

app.get('/',(req,res)=>{
res.render('main');
});

app.post('/register', urlEncodedParser, (req,res)=>{
console.log(req.body);
});

app.listen(3000);

我的表格:

<form action="/register" method="POST">
<input type="text" name='name' class="form-control">
<input type="text" name='surname' class="form-control">
<input type="number" name='age' class="form-control">
<input type="email" name='email' class="form-control">
<input type="password" name='password' class="form-control">
<input type="password" name='confirm' class="form-control">
<input type="submit" name="submit" class="btn btn-block btn-success" value="OKAY">
</form>

我得到的完整错误:

RangeError: Maximum call stack size exceeded
at next (C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\index.js:202:7)
at Layer.handle [as handle_request] (C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\layer.js:97:5)
at trim_prefix (C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\index.js:317:13)
at C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\index.js:335:12)
at next (C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\index.js:275:10)
at jsonParser (C:\xampp\htdocs\express+mongo\node_modules\body-parser\lib\types\json.js:101:7)
at Layer.handle [as handle_request] (C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\index.js:317:13)
at C:\xampp\htdocs\express+mongo\node_modules\express\lib\router\index.js:284:7

最佳答案

问题的原因是您将应用程序作为中间件传递给请求处理程序。具体在这里:

const urlEncodedParser = app.use(bodyParser.urlencoded({extended:false}));

app.post('/register', urlEncodedParser, (req,res)=>{
console.log(req.body);
});

urlEncodedParser 只是对您的应用的另一个引用。因此,您陷入了一个无限循环,其中路由调用 urlEncodedParser 流向您的路由,而您的路由又调用 urlEncodedParser 等等,直到弹出堆栈。

关于javascript - Express 表单发布请求处理中间件返回 RangeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55958218/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com