Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Increase payload in Express.js

This code sends "1" or "2" value as a cookie after pressing submit button:

<form action="/setorder" method="post" class="horizontal">
       <% if (req.cookies.doneAtLast == '1') { %>    
    <input type="hidden" name="done_at_last" value="0">
    <input type="submit" value="Do not sort">
        <% } else {%>
    <input type="hidden" name="done_at_last" value="1">
    <input type="submit" value="Sort">
       <% } %>    
</form>

However, I get 413 error.

req.cookies.doneAtLast is "{ doneAtLast: ‘1’ }" right now.
I tried to increase pyload size :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import { Router, urlencoded, static as staticMiddleware } from 'express';
import methodOverride from 'method-override';
import { mainPage, detailedPage, addPage, add, setDone, remove, setOrder } from "./controllers/todos.js";
import { requestToContext, handleErrors } from './middleware.js';
import { mainErrorHandler, error500Handler } from './error-handlers.js';
import { todoV } from './validators.js';
import cookieParser from 'cookie-parser';
    
    const express = require('express');
    const app = express();
     
    // Route with increased JSON payload limit (50mb)
    app.post('/increased', express.json({ limit: '50mb' }), (req, res) => {
        res.send('Increased JSON payload limit');
      });
    
    const router = require(app).Router();
    
    ...
    router.post('/setorder', setOrder);

But it did not help.

Node.js v20.18.0
[nodemon] app crashed - waiting for file changes before starting...

setOrder controller looks like this:

 export function setOrder(req, res){
  res.cookie('doneAtLast', req.body.done_at_last);
  res.redirect('/');
 } 

I am confused, because I am sending 0 or 1 to the cookies, but express marks it as 413 error.

>Solution :

The 413 error occurs because the server isn’t properly parsing the incoming form data, possibly due to missing middleware or misconfiguration. By adding express.urlencoded middleware and ensuring it’s applied before your routes, you allow the server to parse URL-encoded form data. Additionally, consistent use of module syntax (require vs. import) and checking for other errors will help prevent crashes and ensure your server runs smoothly.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading