Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Connect and Express utils

Connect and Express utils


I'm new in the world of Node.js

According to this topic: What is Node.js' Connect, Express and “middleware”?
I learned that Connect was part of Express

I dug a little in the code, and I found two very interesting files :


and better :


These two files are full of useful functions and I was wondering how to invoke them correctly.

As far, in the ./myProject/app.js, that's what I do:

var express = require('express')
  , resource = require('express-resource')
  , mongoose = require('mongoose')
  , expresstUtils =
  , connectUtils =

But I found it a little clumsy, and what about my others files?

e.g., here is one of my routes:

myResources = app.resource(

and here is the content of myresources.js:

exports.index = function(req, res)
  res.render('./myresources.jade', { title: 'My Resources' });
}; = function(req, res)
  fonction resourceIsWellFormatted(param)
    // Here is some code to determine whether the resource requested
    // match with the required format or not
    // return true if the format is ok
    // return false if not

  if (resourceIsWellFormatted(req.params['myresources']))
    // render the resource
    res.send(400); // HEY! what about the nice Connect.badRequest in its utils.js?

As you can see in the comment after the res.send(400), I ask myself if it is possible to use the badRequest function which is in the utils.js file of the Connect module.

What about the nice md5 function in the same file?

Do I have to place this hugly call at the start of my myresources.js to use them?:

var connectUtils =

or, is there a more elegant solution (even for the app.js)?

Thank you in advance for your help!

Problem courtesy of: Pascal Qyy


the only more elegant way i came up with is (assuming express is inside your root "node_modules" folder):


the node installation is on windows, node version 0.8.2

and a bit of extra information:

this way you don't need to know where you are in the path and be forced to use relative paths (./ or ../), this can be done on any file nesting level.

i put all my custom modules inside the root "node_modules" folder (i named my folder "custom_modules") and call them this way at any level of nesting:

Solution courtesy of: Leonidaz


View additional discussion.

This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Connect and Express utils


Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription