As a server-side developer, it is however necessary to understand the concept of routing and middleware. In this article we’ll emphasize more on E**xpress.JS (**a back-end web application framework for Node.js).

SO WHAT ARE ROUTES?🤔💭

Routes refers to paths for which data travels on a network. It can also be said to be a way taken in getting from a starting point to a destination.

So let’s think of routes this way, if my starting point is “A” and my destination is “B” then the path to which I follow from my starting point “A” to get to destination “B” is my route.

Having known what routes are….

WHY ARE ROUTES CREATED?💭

Routes are created so as to determine the data that should be delivered given any URL.

For instance, when we type in www.google.com in a browser, the response we get is the data which has been determined by a developer(s) which is delivered to the URL www.google.com.

A Demonstration Of How Routes Are Created In Express.JS

LINUS TOLVARDS will say… “TALK IS CHEAP. SHOW ME THE CODE” 😂

First of all let’s create a folder that houses our code. To do so

open up your terminal and get your fingers to work.😊

1. _cd Documents_ 👉(This will take you to document directory, if you're already there jump to step 2).

  1. _mkdir my-first-route_ 👉(This will create a new directory called “my-first-route” inside your Document directory)

  2. _cd my-first-route_ 👉(This will take you to “my-first-route” directory)

  3. _touch app.js_ (This will create a new file called “app.js” in “my-first-route” directory)

  4. _npm init_ 👉(This will let you setup your npm package)

  5. _npm install express — save_ 👉(This will install “express” and save it as a dependency in this project)

Having typed in the command, we should have our files and folders in this structure

The Folder Structure

Folder Structure

so let’s keep it going …

Type in these codes in our app.js file

const app = require("express")();

const PORT = 2000;
app.get("/", (req,res) =>{
res.send("welcome to my homepage!!!!!!");
console.log("welcome page");
});
app.listen(PORT, ()=>{
console.log(`APP IS RUNNING ON PORT ${PORT}...`);
});

Image for post

Writing Our Route

Now to start our server, type in

_node app.js _in the terminal and hit enter

Image for post

Our server is up and running

open up your browser and type in

http://localhost:2000/

Image for post

CONGRATULATIONS… YOU’VE SUCESSFULLY CREATED A ROUTE🤗

#javascript #express #node #web-development #developer

Routes, Middleware in Express.JS
12.20 GEEK