Maximum call stack size exceeded when sending a post request in express

izoo123 :

'RangeError: Maximum call stack size exceeded' when sending a post request, Node.js, body-parser, express

I am new to node.js, I have run into an issue while following video #30 of this tutorial series on node.js and express and I am completely lost as to why it is happening.

I am watching the video about posts, and the tutorial had me set up a app.post for '/contact' page as seen below, but when I submit I get the error seen below. If I comment out the app.post code everything works fine so I assume that is where its breaking.

const express = require('express');
const bodyParser = require('body-parser');
const app = express();

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

app.set('view engine', 'ejs');
app.use('/assets', express.static('assets'));

app.get('/', function(req, res){
  res.render('index');
});

app.get('/contact', function(req, res){
  res.render('contact', {qs: req.query});
});

app.post('/contact', urlencodedParser, function(req, res){
  console.log(req.body);
  res.render('contact', {qs: req.query});
});

app.get('/profile/:id', function(req, res){
  var data = { age: 29, job: "ninja", hobbies: ['eating', 'fighting', 'fishing'] };
  res.render('profile', {person: req.params.id, data: data});
});

app.listen(3000);

/contact page:

<html lang="en" dir="ltr">
  <head>
    <style media="screen">
      body{
        background: skyblue;
        font-family: verdana;
        color: #fff;
        padding: 30px;
      }
      h1{
        font-size: 48px;
        text-transform: uppercase;
        letter-spacing: 2px;
        text-align: center;
      }
      p{
        font-size: 16px;
        text-align: center;
      }
    </style>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <%- include ('partials/nav') %>
    <h1>Contact Us!</h1>
    <p>Contact the coolest cats on the web right here...</p>

    <form id="contact-form" method="POST" action="/contact">
      <label for="who">Who do you want to contact</label>
      <input type="text" name="who" value="<%= qs.person %>">
      <label for="department">Which department?</label>
      <input type="text" name="department" value="<%= qs.dept %>" >
      <label for="email">Your email</label>
      <input type="email" name="email" >
      <input type="submit" name="submit" >
    </form>
  </body>
</html>

Error:

RangeError: Maximum call stack size exceeded
    at /home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/index.js:629:19
    at next (/home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/index.js:210:14)
    at next (/home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/route.js:127:14)
    at Layer.handle_error (/home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/layer.js:67:12)
    at next (/home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/route.js:135:13)
    at /home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/index.js:635:15
    at next (/home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/index.js:260:14)
    at Layer.handle [as handle_request] (/home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/layer.js:97:5)
    at trim_prefix (/home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/index.js:317:13)
    at /home/gavin/Documents/nodeJsTutorials-master/node_modules/express/lib/router/index.js:284:7

BENARD Patrick :

At this point, app.use use to add body parser to each route... But as you can see, you pass it too as middleware in your post route...

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

    app.post('/contact', urlencodedParser, function(req, res){
      console.log(req.body);
      res.render('contact', {qs: req.query});
    });

So... In one hand , remove the app.use

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

    app.post('/contact', bodyParser.urlencoded({ extended: false }), function(req, res){
      console.log(req.body);
      res.render('contact', {qs: req.query});
    });

In the other hand, remove the middleware from your post route

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

    app.post('/contact', function(req, res){
      console.log(req.body);
      res.render('contact', {qs: req.query});
    });

PS:

Or maybe it's just that you're not using the variable of body parser as you should use it and maybe do something like this:

 const urlencodedParser = bodyParser.urlencoded({ extended: false });

    app.post('/contact', urlencodedParser, function(req, res){
      console.log(req.body);
      res.render('contact', {qs: req.query});
    });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=174294&siteId=1