Routing and Node.js

First of all, let’s take a quick (really quick) glance at what routing is:

somewebsite.com/someroute

It’s basically taking the user (or some data) from one place to another.

Routing is basically matches the functions you wrote with the requests that server gets. For get requests rendering the correct page and For post requests handling the post with correct function.

How to route ?

I am not going to show you the easiest way instead I will show you one of the bests practises.

Lets start with our server/app.js file

const routes = require(‘./api/routes’);

app.use(“/”, routes);

Add this lines to your server/app.js file. Here we say that our routes folder will be in /api/routes/ and we say express to use it.

The hierarchy of the routes folder.

api/routes

├── index.js

├── root.js

├── account.js

Every routes folder should have an index.js file. The index.js file does the subrouting process like urls start with “/account” are routed in the account.js etc.

 Content of index.js

const express = require(“express”);

const rootRoutes = require(‘./root’);

const accountRoutes = require(‘./account.js’);

const router = express.Router();

router.use(“/”, rootRoutes);

router.use(“/account”, accountRoutes);

module.exports = router;

Here we say to express that the routes start with “/account ” will be taken care of at account.js file and the other routes will be taken care of at root.js file.

Content of root.js

const express = require(“express”);

const mainController = require(“../controllers/main”);

const router = express.Router();

router

.route(“/”)

.get(mainController.landingPage);

router

.route(“/dashboard”)

.get(mainController.dashboardPage);

module.exports = router;

Here we define one of our controllers as mainController to access its functions. Then we route the get requests for landing page and dashboard page to handling functions in our controller which renders the responding page.

Content of account.js

const express = require(“express”);

const accountController = require(“../controllers/account”);

const router = express.Router();

router

.route(“/login”)

.get(accountController.loginPage)

.post(accountController.userLogin);

router

.route(“/signup”)

.get(accountController.signupPage)

.post(accountController.createUser);

router

.route(“/logout”)

.get(accountController.logout);

module.exports = router;

Here we did thing similar to we did in root.js except handling the post requests.

Routing pathway

We have build an simple routing example. If some one send a get request to /account/login this request first gets handled by server/app.js file then it passes this request to api/routes/index.js file and index.js file handles the subrouting process and passes this request to api/routes/account.js file and finally here this request gets passed to the responding function in controller.

To sum up

server/app.js -> api/routes/index.js -> api/routes/account.js Routing ends

Using ” “” has different effects on express routing so follow my pattern.

Share this post
[social_warfare]
Sample Server Application with Node.js
Node.js Events

Get industry recognized certification – Contact us

keyboard_arrow_up