NodeJS + Express part 5: Routes and Controllers

NodeJS + Express part 5: Routes and Controllers

Here is a series of articles that will allow you to create backend applications with NodeJS + Express.

This series is the continuation of my series on the basics of NodeJS. If you don't have basic knowledge of NodeJS read this the first article of the series.

Node.js is today a must, so it is essential for a developer to master it.

So I will publish a new article about every two days and little by little you will learn everything there is to know about Node.js + Espress

To not miss anything follow me on twitter: twitter.com/EricTheCoder_


Express Router

As you seen in the previous article, creating a CRUD API with Express is easy. But you should have notice by now that all that code in the same file is bulky and we will soon lose ourself.

By convention, Express developper split code in different files and folders to optimized code organisation.

The first and most obvious thing to do is to split the route and the route implementation codes.

Here a exemple we use in the previous article

app.get('/api/products/:productID', (req, res) => {
    const id = Number(req.params.productID)
    const product = products.find(product => product.id === id)

        if (!product) {
        return res.status(404).send('Product not found')
    }
    res.json(product)
})

The goal here will be to split the route and his implementation code. To do that we can use an Express Package call Router

Router help to create a list of all your apps routes and associate them to a controller file that contain the implementation code.

First step is to create a file to put all our app routes. For exemple : /routes/products.js can contain all routes related to the product resource

And next step we will create a controller file to put all our routes implementation code. For exemple: /controllers/products.js

Starting from my previous article here an exemple of a route and a controller file.

routes/products.js

const express = require('express')
const router = express.Router()

const  { 
    getProducts,
    getProduct,
    createProduct,
    updateProduct,
    deleteProduct 
} = require('../controllers/products.js')

router.get('/', getProducts)

router.get('/:productID', getProduct)

router.post('/', createProduct) 

router.put('/:productID', updateProduct) 

router.delete('/:productID', deleteProduct)

module.exports = router

As you can see the implementation is very easy and straightforward.

First create an instance to the Router object

Then import all the controller functions.

Lastly, use the router object to create a route and controller association.

Here is an exemple of the controller functions controllers/products.js

const products = require('../data.js')

const getProducts = ((req, res) => {
    res.json(products)
})

const getProduct = ((req, res) => {
    const id = Number(req.params.productID)
    const product = products.find(product => product.id === id)

        if (!product) {
        return res.status(404).send('Product not found')
    }
    res.json(product)
})

const createProduct = ((req, res) => {
    const newProduct = {
        id: products.length + 1,
        name: req.body.name,
        price: req.body.price
    }
    products.push(newProduct)
    res.status(201).json(newProduct)
})

const updateProduct = ((req, res) => {
    const id = Number(req.params.productID)
    const index = products.findIndex(product => product.id === id)
    const updatedProduct = {
        id: products[index].id,
        name: req.body.name,
        price: req.body.price
    }

    products[index] = updatedProduct
    res.status(200).json('Product updated')
})

const deleteProduct = ((req, res) => {
    const id = Number(req.params.productID)
    const index = products.findIndex(product => product.id === id)
    products.splice(index,1)
    res.status(200).json('Product deleted')
})

module.exports = {
    getProducts,
    getProduct,
    createProduct,
    updateProduct,
    deleteProduct
}

Nothing really new here, it's the same implementation but code are now in a separate functions with a req and res parameter.

Once the routes and controllers files are created we need to tell Express to use those routes

From the main file add a app.use() with our routes reference

const express = require('express')
const app = express()
const products = require('./data.js')
const products_routes = require('./routes/products.js')

app.listen(5000, () => {
    console.log('server is listening on port 5000')
})

app.use(express.json())
app.use('/api/products', products_routes)

Noted, the app.use() have routes prefix in '/api/products' that mean all url path in the routes file will include that prefix automatically.

Conclusion

That's all for today, follow me on twitter: twitter.com/EricTheCoder_ to be notified of the publication of the next article (within two days).

Did you find this article valuable?

Support Eric The Coder Blog by becoming a sponsor. Any amount is appreciated!