EN
Express.js - send JSON in response
3 points
In this article, we would like to show you how to send JSON in response using Express.js.
Quick solution:
xxxxxxxxxx
1
app.get('/path/to/endpoint', (req, res) => {
2
res.json({
3
id: 1,
4
name: 'John'
5
});
6
});
In this example, we present how to use res.json()
method to send JSON as a response.
xxxxxxxxxx
1
const express = require('express'); // npm install express
2
3
const app = express();
4
5
app.get('/path/to/endpoint', (req, res) => {
6
res.json({
7
id: 1,
8
name: 'John'
9
});
10
});
11
12
app.listen(8080, () => console.log('Server started on port 8080.'));