4

我有一个结合了 react.js 前端的 node.js 服务器。我有一个表单,要求用户将文件上传到 AWS S3 存储桶。我正在使用 Multer.js 来处理文件上传。我已将默认上传大小设置为最大 10mb。虽然在使用 localhost 进行测试时,只要文件大小低于 10mb,上传文件绝对没有问题。但是,当我尝试从我的 Nginx Web 服务器执行相同操作时,我在尝试上传任何超过 1mb 的内容时收到“413 错误:请求实体太大”。我试图在这里这里遵循解决方案,但没有运气。

除了我的前端应用程序捕获的 413 错误之外,我没有任何错误输出。

这是我的快速加载程序的代码(引用它是因为我已经在这些代码行上尝试了上面提到的解决方案。)

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;

任何帮助将不胜感激,谢谢!

4

0 回答 0