Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[URGENT]: PayloadTooLargeError: request entity too large #4302

Closed
SebghatYusuf opened this issue Jun 13, 2023 · 2 comments
Closed

[URGENT]: PayloadTooLargeError: request entity too large #4302

SebghatYusuf opened this issue Jun 13, 2023 · 2 comments

Comments

@SebghatYusuf
Copy link

Bug report

Describe the bug

I'm trying to send a payload using POST method to a custom endpoint, and I get PayloadTooLargeError how to extend the default size limit if this is not a bug?

System information

Medusa version (including plugins):
Node.js version:v16.20.0
Database: postgresql
Operating system: macOS 13
Browser (if relevant):

Steps to reproduce the behavior

  1. Create a custom endpoint
  2. Send a post request with a payload more than 1 MB

Expected behavior

There should be a way to extend the post method payload limit.

Code snippets

I tried to pass limit to body parser json function but didn't resolve the issue.

router.post("/customizer/design", json({ limit: '5mb' }), wrapHandler(async (req, res) => {
    const service: CustomDesignService = req.scope.resolve('customDesignService');
    const stat = await service.create(req.body);

    res.status(200).json({
      status: "success",
      stat,
    });
  }));
@pevey
Copy link
Contributor

pevey commented Jun 16, 2023

Is there anything in this thread that helps?

https://stackoverflow.com/questions/31967138/node-js-express-js-bodyparser-post-limit

@SebghatYusuf
Copy link
Author

@pevey, I knew that the problem was with the express server. Even though I was making changes to the index.js file in the base directory, I couldn't see any of the changes applied. This was because when you run medusa develop , it doesn't use the index.js server, causing my code to not work. To resolve the issue, I had to run index.js using node index.js .
Here is the modified index.js that resolved the issue, in case anyone else encounters this problem.

const { json, urlencoded } = require("body-parser");
const express = require("express")
const { GracefulShutdownServer } = require("medusa-core-utils")

const loaders = require("@medusajs/medusa/dist/loaders/index").default;

(async () => {
	async function start() {
		const app = express()
		app.use(json({ limit: '10mb' }))
		app.use(urlencoded({ limit: '10mb', extended: true }))

		const directory = process.cwd()

		try {
			const { container } = await loaders({
				directory,
				expressApp: app
			})

			const configModule = container.resolve("configModule")
			const port = process.env.PORT ?? configModule.projectConfig.port ?? 9000
			const server = GracefulShutdownServer.create(
				app.listen(port, (err) => {
					if (err) {
						return
					}
					console.log(`Server is ready on port: ${port}`)
				})
			)

			// Handle graceful shutdown
			const gracefulShutDown = () => {
				server
					.shutdown()
					.then(() => {
						console.info("Gracefully stopping the server.")
						process.exit(0)
					})
					.catch((e) => {
						console.error("Error received when shutting down the server.", e)
						process.exit(1)
					})
			}
			process.on("SIGTERM", gracefulShutDown)
			process.on("SIGINT", gracefulShutDown)
		} catch (err) {
			console.error("Error starting server", err)
			process.exit(1)
		}
	}

	await start()
})()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants