generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 124
config cds.server.body_parser.limit
(and cds.server
config section)
#1141
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
28252e3
Fix closing parenthesis in code snippets
schwma af493d6
Add server config section for `cors`, `index` and `max_request_body_s…
schwma 5d83e54
Link to "Maximum Request Body Size" section in DOS section
schwma 5fa8924
Merge branch 'main' into maximum-request-body-size
schwma 476602d
Merge branch 'main' into maximum-request-body-size
schwma 886ce9f
Rename `max_request_body_size` -> `body_parser.limit`
schwma 62aec06
Merge branch 'main' into maximum-request-body-size
schwma 04ba102
Add links to CORS and index.html sections and improve wording
schwma e2ead36
Merge branch 'main' into maximum-request-body-size
schwma 278428a
Merge branch 'main' into maximum-request-body-size
schwma b35a5e1
Rename built-in server -> server.js
schwma 303c027
Add code backticks to `server.js` to have consistent formatting
schwma File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,7 +113,7 @@ cds.on('served', ...) | |
### Override `cds.server()` | ||
|
||
Provide an own bootstrapping function if you want to access and process the command line options. | ||
This also allows you to override certain options before delegating to the built-in server.js. | ||
This also allows you to override certain options before delegating to the built-in `server.js`. | ||
In the example below, we construct the express.js app ourselves and fix the models to be loaded. | ||
|
||
```js | ||
|
@@ -213,7 +213,7 @@ In other words this asynchronous handler code does **not work** as expected: | |
```js | ||
cds.on ('bootstrap', async ()=> { | ||
await asyncCode() // [!code error] // will NOT be awaited | ||
} | ||
}) | ||
``` | ||
|
||
You can use the [served](#served) event's asynchronous nature though to wait for such bootstrap code: | ||
|
@@ -222,14 +222,63 @@ You can use the [served](#served) event's asynchronous nature though to wait for | |
let done | ||
cds.on('bootstrap', ()=> { | ||
done = asyncCode() | ||
} | ||
}) | ||
cds.on('served', async ()=> { | ||
await moreCode() | ||
await done | ||
}) | ||
``` | ||
|
||
|
||
|
||
## Configuration | ||
|
||
The behavior of the built-in `server.js` can be customized through the options documented in the following sections. | ||
|
||
### CORS Middleware | ||
|
||
The built-in CORS middleware can be enabled explicitly with `cds.server.cors = true`. By default, this is `false` if in production. | ||
|
||
[Learn more about best practices regarding **Cross-Origin Resource Sharing (CORS)**.](../node.js/best-practices.md#cross-origin-resource-sharing-cors) {.learn-more} | ||
|
||
|
||
|
||
### Toggle Generic Index Page | ||
|
||
The default generic _index.html_ page is not served if `NODE_ENV` is set to `production`. Set `cds.server.index = true` to restore the generic index page in production. | ||
|
||
[See the **Generic *index.html*** page in action.](../get-started/in-a-nutshell.md#generic-index-html) {.learn-more} | ||
|
||
|
||
|
||
### Maximum Request Body Size | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: section name is still "Maximum Request Body Size", but flag name is However, express also explains that its |
||
|
||
There are two ways to restrict the maximum request body size of incoming requests, globally for all endpoints and for individual services. If the payload exceeds the configured value, the request is rejected with _413 - Payload too large_. The configured values are passed through to the underlying Express body parser middlewares. Therefore, the default limit is _100kb_, as this is the default of the Express built-in [body parsers](https://expressjs.com/en/api.html#express.json). | ||
|
||
The maximum request body size can be limited globally, for all services and protocols, using the configuration `cds.server.body_parser.limit`, like so: | ||
|
||
```jsonc | ||
{ | ||
"cds": { | ||
"server": { | ||
"body_parser": { | ||
"limit": "1mb" // also accepts b, kb, etc... | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
To restrict the maximum request body size of requests received by an individual service, the service specific annotation `@cds.server.body_parser.limit` can be used, like so: | ||
|
||
```cds | ||
annotate AdminService with @cds.server.body_parser.limit: '1mb'; | ||
``` | ||
|
||
This is useful when the expected request body sizes might vary for services within the application. If both the global configuration and the service specific annotation are set, the service specific annotation takes precedence for the respective service. | ||
|
||
|
||
|
||
## See Also... | ||
|
||
The [`cds-plugin` package technique](cds-plugins) provides more options to customize server startup. |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.