DEV Community

Cover image for Build a Simple JSON API with Express.js on a Local Server
Pranesh Chowdhury for Pranesh CodeCraft

Posted on Edited on

Build a Simple JSON API with Express.js on a Local Server

Creating a simple server looks like an exciting thing to do. If you're fetching data from other sites, why not create your own server for accessing API data?

It's Easy!!

Image description

Let's learn how to make a local server using express.js for your project by following these steps:

Image description

First, you need to install Node.js. I assume that you have already installed Node.js on your computer.

Install and setup the server:

First, create the backend server:

mkdir my-app-server
⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด ⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด

Go to the folder path:

cd my-app-server
⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด ⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด

npm init:

npm init -y
⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด ⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด

npm install express:

npm install express
⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด ⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด

Install cors:

npm install cors
⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด ⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด

Cors gives access to the server for your project (API call). Otherwise API call in your project will show 'failed to load'.

Then create a file 'index.js'. Now write code in 'index.js'.

const express = require('express');
const app = express();
const cors = require('cors');
const port = process.env.PORT || 5000;

const categories = require('./data/categories.json');

app.use(cors());

app.get('/', (req, res) => {
    res.send("Server is running");
});

app.get('/categories', (req, res) => {
    res.send(categories); 
})

app.listen(port, () => {
    console.log(`Server (categories) is running on port: ${port}`);
})

⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด ⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด

Our data file is categories.json.

Image description

Data of categories.json file:

[
    {
        "id": 1,
        "category": "World",
        "total_news": 150
    },
    {
        "id": 2,
        "category": "Politics",
        "total_news": 120
    },
    {
        "id": 3,
        "category": "Business",
        "total_news": 100
    },
    {
        "id": 4,
        "category": "Technology",
        "total_news": 80
    },
    {
        "id": 5,
        "category": "Sports",
        "total_news": 90
    }
]
⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด ⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด

Run the server:

Run the server using nodemon:

nodemon index.js
⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด ⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด

Nodemon helps to update the server in real-time.

Open your web browser and go to http://localhost:5000. You should see "Server is running" displayed on the page.

Image description

If you go to http://localhost:5000/categories. Then you can see some categories of data.

Image description

Now you can easily access this data for your project.

Image description

That's why I'm here:

YouTube Tutorial: https://youtu.be/u6q5yqlc8bM?si=RQeVvzZRNYdUA0Wc

Thank You.

Image description

Top comments (4)

⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด
 
gfouz profile image
Giovani Fouz •

I think this is a very good introduction. Thanks!

⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด
 
praneshchow profile image
Pranesh Chowdhury Pranesh CodeCraft •

You're welcome. I'm glad you liked it 🩵

⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด
 
fred_functional profile image
Fred Functional •

This is a great tutorial! Just wondering, do you have any tips for debugging common issues with Express.js servers?

⭐ Must Read: praneshcodecraft/building your api server with expressjs mk - สรุปดราม่าล่าสุด
 
praneshchow profile image
Pranesh Chowdhury Pranesh CodeCraft •

Thank you so much, @fred_functional. I mostly use common tools such as console.log(), npm debug, and error handling. I'm continuing to learn in depth about server error handling and debugging. There are a lot of things I need to learn.