3

I have a node.js server combined with a react.js frontend. I've a form that requires users to upload files to a AWS S3 bucket. I'm using Multer.js to handle the file upload. I've set the default upload size to a max of 10mb. While there is absolutely no problem in uploading files as long as the file size below 10mb when testing using localhost. However, when I try to do the same from my Nginx Web Server, I get a '413 Error : Request entity too large' while trying to upload anything that is above 1mb. I've tried to follow the solutions here and here but to no luck.

I don't any error output except the 413 Error that my frontend app catches.

Here's the code to my express loader ( referencing this because I've tried the solutions mentioned above on these lines of code. )

const express = require("express");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const helmet = require("helmet");
const cors = require("cors");
const morgan = require("morgan");
const path = require("path");
const appRoot = require("app-root-path");

const loadRoutes = require("../api");
const logger = require("./logger");
const { userRoles } = require("../utils/consts");

const authService = require("../services/AuthService");

const expressLoader = (app) => {

    app.use(cors());

    app.use(express.static(path.join(appRoot.path, "public", "users")));
    //app.use(express.static(path.join(appRoot.path, "public", "admin")));

    // ATTACH IP ADDRESS TO EACH REQUEST
    app.use((req, res, next) => {
        req.ipAddress = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
        return next();
    });

    // Extract token from header
    app.use((req, res, next) => {

        const token = req.headers["authorization"] ? req.header("authorization").split(" ")[1] : null;

        if (token) {
            req.token = token;
        }

        return next();
    });

    // Verify token
    app.use(async (req, res, next) => {

        if (req.token) {

            const decode = await authService.verifyAuthToken(req.token);

            console.log(decode);

            if (!decode.tokenValid) {

                logger.error(`[INVALID JWT ${req.path}] ip: ${req.ipAddress}`);
                logger.error(decode.err);

                req.isAuth = false;
                return next();

            } else {

                req.isAuth = true;
                req.decode = decode.data;

                return next();
            }
        }

        return next();
    });

    // Check if is admin
    app.use((req, res, next) => {

        const roleId = req.decode ? req.decode.role_id : null;

        if (req.isAuth && (roleId === userRoles.SYSOP || roleId === userRoles.ADMIN)) {

            req.isAdmin = true;
            return next();
        }

        return next();
    });

    app.use(morgan("combined", { stream: logger.stream }));
    app.use(helmet());
    app.use(bodyParser.json({ limit: '50mb' }));
    app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
    //app.use(cookieParser(process.env.SCOOK_SECRET));
    // app.enable("trust proxy"); TO BE ENABLED FOR NGINX

    // LOAD API
    app.use(process.env.API_PREFIX, loadRoutes());

    // app.use("/admin", (req, res, next) => {
    //     logger.info(`[ADMIN ROUTE ACCESSED FROM ${ req.ip }]`);
    //     return res.sendFile(path.join(appRoot.path + "/public/admin/index.html"));
    // });

    app.get("*", (req, res, next) => {
        return res.sendFile(path.join(appRoot.path + "/public/users/index.html"));
    });
}

module.exports = expressLoader;

Any help would be appreciated, thank you!

Abir Rahman
  • 31
  • 1
  • 4
  • 1
    Consider tuning [`client_max_body_size`](http://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size) in your nginx config, for example `client_max_body_size 10m;`. – Ivan Shatsky Feb 03 '20 at 19:15

0 Answers0