expressjsHow can I use Express.js to return a JSON response?
Express.js is a web application framework for Node.js that provides a simple way to return a JSON response. To use Express.js to return a JSON response, you need to set up your server, create a route to handle the request, and send the response in JSON format.
For example:
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send({
message: 'Hello World!'
});
});
app.listen(3000);
The above code will create a server and listen on port 3000. When a request is sent to the /
path, the server will respond with a JSON object containing a message.
The code consists of the following parts:
const express = require('express');
- This imports the Express.js library.const app = express();
- This creates an Express.js application.app.get('/', (req, res) => { ... });
- This creates a route that will handle requests sent to the/
path.res.send({ ... });
- This sends a JSON response containing the messageHello World!
.
For more information, see the Express.js documentation.
More of Expressjs
- How do I set the time zone in Express.js?
- How can I use Express.js to generate a zip response?
- How do I use Yarn to add Express.js to my project?
- How do I find Express.js tutorials on YouTube?
- How can I use Node.js and Express together to create a web application?
- How can I use Express.js to yield results?
- How can I use an ExpressJS webhook to receive data from an external source?
- How can I use Express.js and Winston together to create a logging system?
- How do I use Express.js to handle x-www-form-urlencoded data?
- How can I use Express.js with TypeScript?
See more codes...