,

How to Set Up and Render a 3-Minute REST API in Our Express Js With 4K

Posted by

3 Minute Rest API Setup and Render In Our Express Js With 4K

3 Minute Rest API Setup and Render In Our Express Js With 4K

In this article, we will discuss how to set up a REST API in just 3 minutes and render it in our Express Js with 4K resolution.

Setting up the REST API

To set up the REST API, we will be using a tool called json-server. Json-server allows us to quickly create a REST API with just a few lines of code. First, we need to install json-server using the following command:
npm install -g json-server

Once json-server is installed, we can create a new file called db.json and define our data model. For example:

            {
                "users": [
                    { "id": 1, "name": "John Doe" },
                    { "id": 2, "name": "Jane Smith" }
                ]
            }
        

After defining the data model, we can start the json-server by running the following command in the terminal:
json-server --watch db.json

Our REST API is now up and running at http://localhost:3000/users

Rendering the REST API in Express Js with 4K

Now that we have our REST API set up, we can render it in our Express Js application. First, we need to install the following npm packages:
npm install express axios ejs

After installing the packages, we can create a new file called index.js and set up our Express Js application to render the REST API data in 4K resolution using EJS as the view engine. For example:

            const express = require('express');
            const axios = require('axios');
            const ejs = require('ejs');

            const app = express();

            app.set('view engine', 'ejs');

            app.get('/', async (req, res) => {
                const { data: users } = await axios.get('http://localhost:3000/users');
                res.render('index', { users });
            });

            app.listen(3000, () => {
                console.log('Server started on port 3000');
            });
        

In this example, we are making a GET request to our REST API using axios and rendering the data in the index.ejs file. We can then create the index.ejs file with the following code to render the user data in 4K resolution:

            
            
            
                
                
                
                Users
            
            
                

Users

    { %>

Now, when we visit http://localhost:3000, we will see the user data rendered in 4K resolution using Express Js and EJS.

In conclusion, we have successfully set up a REST API in just 3 minutes and rendered it in our Express Js application with 4K resolution. This allows us to quickly and easily integrate external data into our web applications, providing a seamless experience for our users.

0 0 votes
Article Rating
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@fireshipio
6 months ago

Subscribe like and. Share