Increase default payload size

How can I increase the default size of the payload, this is for when I am uploading larger file. Can I create some custom middleware in a new file which I can tell wasp to use, instead of express default?
3 Replies
kapa.ai
kapa.ai•3d ago
Hi @johnsharp.. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To increase the default payload size for file uploads in Wasp, you can indeed create custom middleware. Wasp allows you to configure middleware, including overriding the default Express middleware. Here's how you can do it: 1. First, create a new file for your custom middleware, for example, src/serverSetup.js (or src/serverSetup.ts for TypeScript). 2. In this file, define a middleware configuration function:
import express from 'express'

export const serverMiddlewareFn = (middlewareConfig) => {
// Remove the default express.json middleware
middlewareConfig.delete('express.json')

// Add custom middleware with increased payload size
middlewareConfig.set('express.json', express.json({ limit: '50mb' }))

// You can also increase the limit for URL-encoded data if needed
middlewareConfig.set('express.urlencoded', express.urlencoded({ extended: true, limit: '50mb' }))

return middlewareConfig
}
import express from 'express'

export const serverMiddlewareFn = (middlewareConfig) => {
// Remove the default express.json middleware
middlewareConfig.delete('express.json')

// Add custom middleware with increased payload size
middlewareConfig.set('express.json', express.json({ limit: '50mb' }))

// You can also increase the limit for URL-encoded data if needed
middlewareConfig.set('express.urlencoded', express.urlencoded({ extended: true, limit: '50mb' }))

return middlewareConfig
}
3. Then, in your main.wasp file, configure the server to use this custom middleware:
app todoApp {
// ...
server: {
middlewareConfigFn: import { serverMiddlewareFn } from "@src/serverSetup"
},
}
app todoApp {
// ...
server: {
middlewareConfigFn: import { serverMiddlewareFn } from "@src/serverSetup"
},
}
This approach allows you to override the default Express middleware with your custom configuration, increasing the payload size limit to 50MB in this example. You can adjust the limit value as needed for your use case. For more details on customizing middleware in Wasp, you can refer to the Configuring Middleware documentation (https://wasp-lang.dev/docs/advanced/middleware-config#1-customize-global-middleware). If you're specifically dealing with file uploads, you might also want to consider using a library like Multer, which is designed for handling multipart/form-data. You can integrate Multer into your custom middleware configuration as well. For an example of how to use Multer with Wasp, check out this GitHub issue comment (https://github.com/wasp-lang/wasp/issues/1683#issuecomment-1308324486). Remember that increasing payload size can have implications for server performance and security, so be sure to set appropriate limits based on your specific requirements. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @johnsharp., I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
johnsharp.
johnsharp.OP•3d ago
Thanks, that worked
miho
miho•3d ago
Nice đź‘Ť
Want results from more Discord servers?
Add your server