Home Ask Login Register

Developers Planet

Your answer is one click away!

shopia T February 2016

How to organize express middlewares better?

I'm using node with express and the following code is working as expected.

file app.js

app.use(function (req, res, next) {
    fs.mkdir('uploads/', function (e) {
        if (!!e && e.code !== 'EEXIST') {
            console.log('Error while trying to create upload folder: ' + err);
}, upload.single('file'));

My question is whether there is better way to write it? I mean to separate the function logic to other file and require it?


Danila Shutov February 2016

I whould suggest to create file '{project_root}/middlewares/fsUtils.js':

function createFolderIfNotExists(folder) {
    return function (req, res, next) {
        fs.mkdir(folder, function (e) {
            if (!!e && e.code !== 'EEXIST') {
                console.log('Error while trying to create upload folder: ' + err);

module.exports = {
    createFolderIfNotExists: createFolderIfNotExists

Then you can use it like:

var fsUtils = require('./middlewares/fsUtils');

app.use(fsUtils.createFolderIfNotExists('/uploads'), upload.single('file'));

Not an ideal approach though. It is better to move initialization logic to separate script and run it at application startup, so that at the moment when you are expecting 'uploads' folder to exist in the filesystem - it will be there already.

zero298 February 2016

This sort of extends Danila Shutov's answer. I would also suggest that you learn to use express.Router(). These can help you organize your routes a little better by making sub-routes refactorable to single files.

Just using require

Consider the single file below, where you define all your routes but import individual handlers with a require. It cleans it up a bit, but you're still going to have a mountain of routes defined in a single file. It will also be a hassle if you ever change the path of those routes.


app.use("/", require("./root-route"));
app.use("/users", require("./users-route"));
app.use("/users/create", require("./users-create-route"));
app.use("/users/delete", require("./users-delete-route"));
app.use("/comments", require("./comments-route"));
app.use("/comments/create", require("./comments-create-route"));
app.use("/comments/delete", require("./comments-delete-route"));

Using express.Router()

Now consider this multi-file solution that uses express.Router(). None of the routers actually have to know about one another or even be in the same file. Additionally, you can bind the router to whatever path you want and then switch it without having to do a find/replace on your whole project.

For example, if we didn't actually want users to be the path to whatever logic ./users-route does, we could mount it at players and it would automatically work.


var usersRoute = require("express").Router();

usersRoute.use("/", require("./users-route"));
usersRoute.use("/create", require("./users-create-route"));
usersRoute.use("/delete", require("./users-d 

Post Status

Asked in February 2016
Viewed 2,241 times
Voted 10
Answered 2 times


Leave an answer

Quote of the day: live life