Languages
[Edit]
EN

Express.js - send JSON in response

3 points
Created by:
FryerTuck
649

In this article, we would like to show you how to send JSON in response using Express.js.

Quick solution:

app.get('/path/to/endpoint', (req, res) => {
    res.json({
        id: 1,
        name: 'John'
    });
});

 

Practical example

In this example, we present how to use res.json() method to send JSON  as a response.

const express = require('express');  // npm install express

const app = express();

app.get('/path/to/endpoint', (req, res) => {
    res.json({
        id: 1,
        name: 'John'
    });
});

app.listen(8080, () => console.log('Server started on port 8080.'));

 

See also

  1. Node.js - send JSON in response

References

  1. Express 4.x - API Reference - res.json()

Alternative titles

  1. Express.js - return JSON in response
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join