Saturday, 10 June 2017

Express: receiving a router instance vs creating one on routes file

I'm grasping the basis of Express, and I'm wondering whether there's a difference or a standard choice with an objective reason for choosing one of these approaches when dealing with routes:

import { Router } from 'express';

const router = new Router();

router.get('/', (req, res) => res.send('hello world'));

export default router;

VS

export default function(router) {
    router.get('/', (req, res) => res.send('hello world'));
}

The first approach I took from the mern.io stack, and the second one from the krakenjs example.

At first glance (and having a OOP background) it looks like the second approach is designed for dependency injection; but since we're talking about javascript, I'm not sure the first one isn't. Also I'm not sure how testing is done, so maybe both cases are testables.

Any insights regarding which approach is considered the standard way and why would be appreciated.



via Christopher Francisco

No comments:

Post a Comment