Intro to using Express.js Router

When we start bringing in routes to Express.JS, we want to separate routes into another folder.

This what our file structure should look like

___________________
> Node_modueles
> Routes
— — — home.js
— — — users.js
app.js
package-lock.json
package.json

_____________________

In each route file, we will want to import express and Router()

const express = require(‘express’);
//this is like createing a mini express app, or a pluggin
const router = express.Router();
//syntax include, remeber that all of these are functions.
router.use; //important to keep in mind that this does not to an exact route match
//get, post, put, delete will all do and exact route match
router.get;
router.post;
router.put;
//not one hundered on this but I’m sure that destroy is something like this
router.delete;
module.exports = router;

To use our new route files, we have to import them and call them with the use() express method.

//IN app.js
const express = require('express');
const app = express();
const myRoute = require('./routes/<file name>');
const anotherRoute = require('./routes/<another file name>')
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({extended: false}))
app.use(myRoute)
app.use(anotherRoute)
app.listen(3000)//easy peasy

Let us make a 404 error.

//js executes from top to bottom
...
app.use((req, res, next)=>{
res.status(404).send('<h1>Page not found</h1>');
})
//we can always chain res whit .status(code number) or .setHeaders(header)
app.listen(3000)

filtering Paths

Another cool feature of express.js is we can filter paths.

//all we have to do is add and argument to app.use('/route', routeName)
...
app.use('/admin', adminRoutes)
app.listen(3000)
// keep in mind this only works if your have to routes with the same path but not the same method like so
router.get('/add-product', (req, res, next) => {
res.send('<form action="/admin/add-product" method="POST"><input type="text" name="title"><button>Add Product</button></form>')
});
//notice that the acton still uses /admin that is becuse we will still be going to /admin/add-product
router.post('/add-product', (req, res, next) => {
console.log((req.body.title))
res.redirect('/')
})
//again we redirect to /admin/add-product

--

--

--

I love tackling new and exciting challenges and working with software because it’s consistently changing, which is exciting.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The concept of ‘this’ in Javascript

Array/Object Destructing in JavaScript

IBM Cloud Speech-to-Text: Two Implementations

What are WebSockets ?

Understanding redux-saga: From action creators to sagas

Adding React to (a legacy) ASP .NET MVC project

How to run and build webpack

Unit and Integration tests for Angular components. Part 1 out of 3.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Cameron J. Leverett

Cameron J. Leverett

I love tackling new and exciting challenges and working with software because it’s consistently changing, which is exciting.

More from Medium

Simple Web Server (NodeJs + Express) + Apache2

Nodejs module and export shenanigans

image_credit — NodeJS — The Complete Guide (incl. MVC, REST APIs, GraphQL)

How To Make Your NodeJS Application Secure?

Nodejs handling POST requests.