If you have any query feel free to chat us!
Happy Coding! Happy Learning!
To render an API, you need to create a route that will respond to HTTP requests with data in a particular format such as JSON. Here's an example of how you can render an API using Node.js and Express:
javascriptCopy code
const express = require('express');
const app = express();
const products = [
{ id: 1, name: 'Product 1', price: 10 },
{ id: 2, name: 'Product 2', price: 20 },
{ id: 3, name: 'Product 3', price: 30 }
];
app.get('/api/products', (req, res) => {
res.json(products);
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Listening on port ${port}...`);
});
In the example above, we have created an Express app and defined an array of products. We then created a route to handle GET requests to the URL /api/products
. When a GET request is made to this URL, the server responds with the products array in JSON format using the res.json()
method.
To test this API, you can use a tool like Postman to make a GET request to http://localhost:3000/api/products
, which will return the array of products in JSON format.
Comments: 2
I am not able to access videos from second class and further. I have already completed first class
When will I get my course?
Now, Your query was resolved.