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

Connect and Express utils

Connect and Express utils

Problem

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 :

./myProject/node_modules/express/lib/utils.js

and better :

./myProject/node_modules/express/node_modules/connect/lib/utils.js

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 =
      require('./node_modules/express/lib/utils.js');
  , connectUtils =
      require('./node_modules/express/node_modules/connect/lib/utils.js');

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

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

myResources = app.resource(
                'myresources',
                require('./routes/myresources.js'));

and here is the content of myresources.js:

exports.index = function(req, res)
{
  res.render('./myresources.jade', { title: 'My Resources' });
};

exports.show = 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
  }
  else
  {
    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 =
      require('../node_modules/express/node_modules/connect/lib/utils.js');

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

Thank you in advance for your help!

Problem courtesy of: Pascal Qyy

Solution

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

require("express/node_modules/connect/lib/utils");

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:

require("custom_modules/mymodule/something")
Solution courtesy of: Leonidaz

Discussion

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

×