Separating Routes and Middleware into Files (in Express.js)

Timo

I'd like to separate routes and middleware out of my main app.coffee-file. (I'm using Express.js.)

This file has grown extensively and is not easy to skim anymore.

How can I put those into separate files so they are still aware of each other?

Example of route that uses middleware:

app.post '/signup', signup.inputsOK, signup.userIsNew, signup.createUser, allowUser, (req, res) ->
  res.redirect '/'

Every middleware function should be available for each route and each route should be available in the main app.coffee-file!

Thanks for ideas!

RyanWilcox

Remember that Express middlewares are just functions, so as long as the function is required it can be used.

user_routes.coffee

hello_user = ( req, res, next ) ->
    res.json {"message": "hi"}

module.exports = {
    hello_user : hello_user
}

app.coffee

user_routes = require './user_routes' 
app.post '/signup', signup.inputsOK, signup.userIsNew, signup.createUser, allowUser, user_routes.hello_user

I would do the middleware setup like this: the app.coffee file is responsible for setting up the route with the middlewares and the actual business logic function... then all the last function needs to do is the actual work required. (This also makes the function easier to unit test, without having to fake out Express routing)

Alternatively with a little work you could separate out the routes and have each file do the app.add work. (I dislike this approach, but many apps do this).

user_routes.coffee

setupUserRoutes = (app) ->
    app.post '/signup', signup.inputsOK, signup.userIsNew, signup.createUser, allowUser, (req, res) ->
      res.redirect '/'

module.exports = setupUserRoutes

app.coffee

app = express()

require( './user_routes' )(app)

Here you're passing the Express object around to each new module you import. Since you set you exports to be only the one function - which takes one parameter, the created Express object - you can create your routes with the required middleware

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Express JS - Use of anonymous functions for routes and middleware

From Dev

Move routes into files in Express.js

From Dev

Move routes into files in Express.js

From Dev

Passing parameters to express middleware in routes

From Dev

Using express middleware in Sails.js to serve static files

From Dev

Express.js 4 - use middleware for authentication before static files

From Dev

Node Express 4 middleware after routes

From Dev

Routes in express JS

From Dev

Express JS Routes

From Dev

Error on separating the router file in express js 4

From Dev

Express Routes - Issues with multiple files

From Dev

Express Routes - Issues with multiple files

From Dev

passing the express app to the routes files

From Dev

Nodejs Express: Routes in separate files

From Dev

express js multiple app middleware

From Dev

Express js - Error handling in middleware

From Dev

Express js - Error handling in middleware

From Dev

Express js:middleware function is not invoked

From Dev

Express.js dynamic routes

From Dev

Express js routes - overlapping params

From Dev

Express.js routes with Typescript

From Dev

Consolidating Routes in Express.js

From Dev

Run express middleware for all routes except that are starting with /api?

From Dev

Is node passport custom callback as middleware possible for express routes?

From Dev

Is node passport custom callback as middleware possible for express routes?

From Dev

Node Express.js access variables from routes declared in separate files

From Dev

How to get different css files for different routes (express, node.js)

From Dev

how to get Express routes to angularjs files working

From Dev

Express routes & Same Object needed in several files

Related Related

HotTag

Archive