Questions & Answers

How can I return JSON responses?

Deepkit HTTP
50 up-votes
Warning: The answer is generated by an artificial intelligence. It might not be correct.
To adjust rating, open the thread in Discord and click on the up/down vote button.

To return JSON responses, you can simply return a JavaScript object from your route handler. Deepkit will automatically serialize the object to JSON and set the appropriate Content-Type header. If you specify a return type for your route handler, Deepkit will use this type to serialize the response (and omit anything not specified in this type).

Here's an example:

router.get('/', (): {message: string} => {
  return { message: 'Hello World' };
});

This will return the following JSON response:

{
  "message": "Hello World"
}