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).
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.
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.
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).
_mkdir my-first-route_ 👉(This will create a new directory called “my-first-route” inside your Document directory)
_cd my-first-route_ 👉(This will take you to “my-first-route” directory)
_touch app.js_ (This will create a new file called “app.js” in “my-first-route” directory)
_npm init_ 👉(This will let you setup your npm package)
_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
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}...`);
});
Writing Our Route
Now to start our server, type in
_node app.js _
in the terminal and hit enter
Our server is up and running
open up your browser and type in
CONGRATULATIONS… YOU’VE SUCESSFULLY CREATED A ROUTE🤗
#javascript #express #node #web-development #developer