// Import 'express' module
const express = require('express');
const app = express();
// Define a route for accessing a resource
app.get('/api/resource', (req, res) => {
res.json({ message: 'Resource data' });
});
// Start the server and listen on port 3000
app.listen(3000, () => {
console.log('RESTful API is running on port 3000');
});
code
Simple RESTful API in Node.js using Express
This Node.js code sets up a simple RESTful API using Express. It defines a route ('/api/resource') that responds with JSON data when accessed. The server is started and listens on port 3000. The comments provide explanations for each part of the code.
Node.js
Express.js
RESTful API