question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Express like Router Object

See original GitHub issue

Hi, first of all, thank you very much for this project and for being so kind when replying to the issues here.

With express js, I use router objects outside the main server.js file; from this file I require all the router objects and apply them by using app.use('/prefix', routerObj).

My main concern is code organization but I don’t know if using router objects is the best way to do it with express.

Anyways, it seems restana does not implement a Router Object like express (does it?).

So, given all this, do you have any suggestions for code organization using restana?

Thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
campiecommented, Dec 23, 2019

Hi @jkyberneees,

I’m closing this issue because everything is working!

Thank you for suggesting the middleware-if-unless module. It’s definitely useful for my use case.

Instead of using middleware-if-unless though, I have refactored my loadRoutes function so that every request handler function receives four arguments: app, path, middlewares (array) and context (an object into which I can inject a DB reference, for example).

routes/api/getAllUsers.js

module.exports = function (app, path, middlewares, context) {
    app.get(path, ...middlewares, (req, res) => {
        res.send(context.db.users.getAll())
    })
}

server.js

const restana = require('restana')
const loadRoutes = require('./lib/app/loadRoutes')
const connectDb = require('./db')
const cookieParser = require('cookie-parser')
const bodyParser = require('body-parser')
const authMid = require('./middlewares/auth')
const limitById = require('./middlewares/limitById')

const run = async () => {

    const db = await connectDb()

    const context = {
        db
    }

    loadRoutes({
        app,
        folder: '/api',
        middlewares: [
            cookieParser(),
            authMid,
            limitById,
            bodyParser.json()
        ],
        context
    })

    loadRoutes({
        app,
        folder: '/auth',
        middlewares: [
            bodyParser.json()
        ],
        context
    })

    console.log(app.routes())

    app.start(process.env.APP_PORT)
}

run().catch(console.log)

I’m looking forward to playing with restana version 4.

Thank you very much for all you’ve been doing!

0reactions
jkyberneeescommented, Dec 23, 2019

You rock!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Routing - Express.js
Use the express.Router class to create modular, mountable route handlers. A Router instance is a complete middleware and routing system; for this reason,...
Read more >
Express Tutorial Part 4: Routes and controllers - MDN Web Docs
First we create routes for a wiki in a module named wiki.js. The code first imports the Express application object, uses it to...
Read more >
Express.js express.Router() Function - GeeksforGeeks
The express.Router() function is used to create a new router object. This function is used when you want to create a new router...
Read more >
Guide to the Express Router Object — Request and ...
The Expressrouter object is a collection of middlewares and routes. It a mini-app within the main app. It also behaves like middleware ...
Read more >
Express Router: How to Use Routing in Node.js - AppDividend
Express Router is a built-in class that refers to how an application's endpoints (URIs) respond to client requests. In addition, the express ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found