413有效载荷太大 - 无法使用其他解决方案解决

发布于 2025-01-20 11:53:26 字数 3977 浏览 0 评论 0原文

我零星地遇到此错误,修复它,然后再次得到它。我按照指示在

import express from 'express'
import cors from 'cors'
import dotenv from 'dotenv'
import { ApolloServer } from 'apollo-server-express'
import typeDefs from './graphql/typeDefs.js';
import resolvers from './graphql/resolvers/index.js';
import { uploadFile, getFileStream } from './s3/s3.js'
import multer from 'multer'
import fs from 'fs'
import util from 'util'
import sendForgotPasswordEmail from './utils/twoFactorAuth/forgotPasswordEmail.js'
import { pdfToExcel, parseExcel } from './utils/pdfParser/pdfParser.js'

dotenv.config();

const startApolloServer = async () => {
    const app = express()
    const upload = multer({
        dest: 'uploads/'
    })

    const storage = multer.diskStorage({
        destination: (req, file, cb) => {
          cb(null, "uploads/")
        },
        filename: (req, file, cb) => {
          cb(null, Date.now() + "-" + file.originalname)
        },
      })
    const uploadStorage = multer({ storage: storage })

    const unlinkFile = util.promisify(fs.unlink)

    const server = new ApolloServer({
        typeDefs,
        resolvers,
        context: ({ req }) => ({ req })
    })

    const whitelist = [
        "http://localhost:3000",
        "http://localhost:5001/graphql",
        "https://studio.apollographql.com",
        "http://localhost:8000",
        "http://localhost:8080"
    ]

    app.use(cors({
        /* credentials: true, */
        origin: "*"
    }));


    app.use(express.json({ limit: '1000kb' }))
    app.use(express.urlencoded({
        extended: true,
        limit: '1000kb'
    }));

    // When hitting the backend domain only, you will get a welcome message to show that the backend is working
    app.get('/', (req, res) => {
        res.send('Welcome to SQL');
    });

    // This would reset a specific driver's password
    app.get('/password/reset/:driverId', async (req, res) => {
        sendForgotPasswordEmail(req.params.driverId)
        res.send(200)
    })

    // This is how image files will be recieved 
    app.get('/images/:key', async (req, res) => {
        const key = req.params.key
        const readStream = await getFileStream(key)
        readStream.pipe(res)
    })

    // This is how image files get saved
    app.post('/images', upload.single('image'), async (req, res) => {
        const file = req.file
        const result = await uploadFile(file)
        await unlinkFile(file.path)
        res.send({
            imagePath: `/images/${result.Key}`
        })
    })

    // This is how the scorecard will be parsed via PDFTables
    app.post('/pdfparse', uploadStorage.single("file"), async (req, res) => {
        const file = req.file
        await pdfToExcel(file)

        setTimeout(async () => {
            const parseData = await parseExcel(req.file)
            await res.send(parseData)
        }, 10000)
    })

    // This is how excel document
    app.post('/excelparse', uploadStorage.single("file"), async (req, res) => {
        const parseData = await parseExcel(req.file)
        await res.send(parseData)
    })

    // starts the server
    await server.start()

    // Applies the following middlewares to the newly spun up server
    await server.applyMiddleware({
        app,
        path: '/graphql',
        cors: false,
        bodyParserConfig: {
            limit: '700kb'
        }
    });
    await app.listen(process.env.PORT, () => console.log(`Server running on ${process.env.PORT}... GraphQL/Apollo at studio.apollographql.com/dev`));
}

startApolloServer()

重置

app.use(express.json({ limit: '1000kb' }))
app.use(express.urlencoded({
    extended: true,
    limit: '1000kb'
}));
await server.applyMiddleware({
    app,
    path: '/graphql',
    cors: false,
    bodyParserConfig: {
        limit: '700kb'
    }
});

我认为以下几行将解决我的问题,但是在此过程中的某个地方,limit大小必须根据此代码 ,有人有可能的解决方案吗?

I keep sporadically getting this error, fixing it, and then getting it again. I followed the instructions for the highest voted answer on this following question, and it didn't work unfortunately. I have a pretty concise server.js file, so I'm unsure why the very detailed explanation listed isn't working for me. Here is the code that sets up the server and body-parser

import express from 'express'
import cors from 'cors'
import dotenv from 'dotenv'
import { ApolloServer } from 'apollo-server-express'
import typeDefs from './graphql/typeDefs.js';
import resolvers from './graphql/resolvers/index.js';
import { uploadFile, getFileStream } from './s3/s3.js'
import multer from 'multer'
import fs from 'fs'
import util from 'util'
import sendForgotPasswordEmail from './utils/twoFactorAuth/forgotPasswordEmail.js'
import { pdfToExcel, parseExcel } from './utils/pdfParser/pdfParser.js'

dotenv.config();

const startApolloServer = async () => {
    const app = express()
    const upload = multer({
        dest: 'uploads/'
    })

    const storage = multer.diskStorage({
        destination: (req, file, cb) => {
          cb(null, "uploads/")
        },
        filename: (req, file, cb) => {
          cb(null, Date.now() + "-" + file.originalname)
        },
      })
    const uploadStorage = multer({ storage: storage })

    const unlinkFile = util.promisify(fs.unlink)

    const server = new ApolloServer({
        typeDefs,
        resolvers,
        context: ({ req }) => ({ req })
    })

    const whitelist = [
        "http://localhost:3000",
        "http://localhost:5001/graphql",
        "https://studio.apollographql.com",
        "http://localhost:8000",
        "http://localhost:8080"
    ]

    app.use(cors({
        /* credentials: true, */
        origin: "*"
    }));


    app.use(express.json({ limit: '1000kb' }))
    app.use(express.urlencoded({
        extended: true,
        limit: '1000kb'
    }));

    // When hitting the backend domain only, you will get a welcome message to show that the backend is working
    app.get('/', (req, res) => {
        res.send('Welcome to SQL');
    });

    // This would reset a specific driver's password
    app.get('/password/reset/:driverId', async (req, res) => {
        sendForgotPasswordEmail(req.params.driverId)
        res.send(200)
    })

    // This is how image files will be recieved 
    app.get('/images/:key', async (req, res) => {
        const key = req.params.key
        const readStream = await getFileStream(key)
        readStream.pipe(res)
    })

    // This is how image files get saved
    app.post('/images', upload.single('image'), async (req, res) => {
        const file = req.file
        const result = await uploadFile(file)
        await unlinkFile(file.path)
        res.send({
            imagePath: `/images/${result.Key}`
        })
    })

    // This is how the scorecard will be parsed via PDFTables
    app.post('/pdfparse', uploadStorage.single("file"), async (req, res) => {
        const file = req.file
        await pdfToExcel(file)

        setTimeout(async () => {
            const parseData = await parseExcel(req.file)
            await res.send(parseData)
        }, 10000)
    })

    // This is how excel document
    app.post('/excelparse', uploadStorage.single("file"), async (req, res) => {
        const parseData = await parseExcel(req.file)
        await res.send(parseData)
    })

    // starts the server
    await server.start()

    // Applies the following middlewares to the newly spun up server
    await server.applyMiddleware({
        app,
        path: '/graphql',
        cors: false,
        bodyParserConfig: {
            limit: '700kb'
        }
    });
    await app.listen(process.env.PORT, () => console.log(`Server running on ${process.env.PORT}... GraphQL/Apollo at studio.apollographql.com/dev`));
}

startApolloServer()

I believed that the following lines would have solved my issue, but somewhere during the process, the limit size must be getting reset

app.use(express.json({ limit: '1000kb' }))
app.use(express.urlencoded({
    extended: true,
    limit: '1000kb'
}));
await server.applyMiddleware({
    app,
    path: '/graphql',
    cors: false,
    bodyParserConfig: {
        limit: '700kb'
    }
});

Based on this code, does anyone have a solution that may be possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文