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:
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.'));