Step-by-Step Guide to Building Your First Route in Express and Node.js

RIMANSHU SINGHRIMANSHU SINGH
3 min read

Install Node.js

First, we have to download and install Node.js from it's official website.

.For download Node.js and installation process click on below link and follow that Article.

According to your computer πŸ–₯, windows user download for windows same linux and MacOS download for there respected Device.

Step towards Project

Step 1- Create a Folder anywhere in your local Machine, then open that Folder in VS Code. After Open that folder, Open Terminal in your VS Code and run this Command.

npm init

This command initialize node in your folder and create a package.json file. which ask some question to you.

After click enter, it ask some question and modification if you want in your project, e.g- package name, version, author keyword, description and many more. In the same rows in ask for entry point (index.js) you just update it and write (src/index.js), same as shows below in Screenshot.But If you dont want to change default setting click enter, enter, enter…..

After Complete this step run this command to Install neccessary Modules and dependency.

npm i express

After press Enter, It start installing some node file in your directory that are necessary for your project. Then go to your folder and Create some Files and Folder.

create a folder inside root directory named src, go inside src folder and create another folder named - routes and inside src create a file named - index.js,

Index.js page is the entry point/page for your Project as you set while initialize npm. In the index page import express and then store it in app named variable.

After this, listen the app to start the server.

const express = require("express");
const app = express();
const PORT = 4004;
try{
    app.listen(PORT || 4004, ()=>{
        console.log(`πŸ›ž Server Running on Port: ${PORT}`);
    })
} catch(error){
    console.log("Failed to start Server", error);
}

Now create a file in routes folder πŸ“‚ and name it userRoutes.js

Import express in userRoutes.js and then create a variable named router that store the property of express.Router() method after it create a router using post method that send a string in response. Now export Router.

const express = require("express");
const router = express.Router();

router.post("/home", (req,res)=>{
    res.send("This is Home Page");
})

module.exports= router

After completing the routes file go to the index.js page. import userRoute and then create a ain route that serve userRoute

const userRoutes = require('./routes/userRoutes.js')
const express = require("express");
const app = express();
const PORT = 4004;
try{
    app.listen(PORT || 4004, ()=>{
        console.log(`πŸ›ž Server Running on Port: ${PORT}`);
    })
} catch(error){
    console.log("πŸ’₯ Failed to start Server", error);
}

app.use("/api", userRoutes) // this route serve the userRoutes.
// e.g - http://localhost:4004/api/home

/api is the main route that serve all the route of userRoutes. And /home is the nested route that inside the userRoutes. Then open the postman and send the url along with route. http://localhost:4004/api/home use post method or whatever you define while creating route. Now you get Response of This is Home Page

3
Subscribe to my newsletter

Read articles from RIMANSHU SINGH directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

RIMANSHU SINGH
RIMANSHU SINGH

I'm a passionate frontend developer with a knack for crafting immersive user experiences. Over the years, I've honed my skills in ReactJS, MongoDB, Redux, React-Toolkit, and SQL, leveraging these tools to deliver robust and dynamic web applications. I am passionate about explore and learning new things and willing to work beyond my capacity to administer projects. πŸ’Ό Currently, Diving deep into MEAN stack