Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :

./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!

like image 365
Pascal Qyy Avatar asked Oct 08 '22 07:10

Pascal Qyy


2 Answers

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")
like image 139
Leonidaz Avatar answered Oct 12 '22 20:10

Leonidaz


If you want to access connect directly, I suggest you install connect as a dependency of your project, along with express. Then you can var utils = require('connect').utils.

like image 30
Linus Thiel Avatar answered Oct 12 '22 20:10

Linus Thiel