It's a simple npm module, so you can either install it using npm,
$ npm install connect-commonjs-amd
or, you can add it to your
This middleware works by determining if a requested file even exists. If it doesn't, then it will look for it in another directory that you specify.
var express = require'express'path = require'path'app = expresspublicFolder = pathjoin__dirname 'public';appuserequire'connect-commonjs-amd'src: pathjoin__dirname 'src'dest: pathjoin__dirname 'public';appuseexpressstaticpublicFolder;applisten3000;console.log"Server listening on port 3000";
So everytime a user requests a
js/script.js, and the middleware isn't able to find it in
./public/js, then it will look in
If you want to try it out, here's how. I'm going to assume that your source tree looks like so.
- server.js (the above server code)
- require.js (assuming that you use RequireJS as the AMD loader)
Here's what your
public/index.html might look like.
And perhaps your
public/src/app/script.js might look like this.
var foo = require'app/foo';alertfoogreeting;;
public/src/app/foo.js might look like this.
moduleexportsgreeting = "Hello, World!";
Start your server and go to your browser. You should see an alert box show with the string "Hello, World!".
You can find a slightly modified version of the above example, here.
connect-commonjs-amd has one function that you can call without using the module as a middleware.
toCommonjs is that function.
var connectCommonJsAmd = require'connect-commonjs-amd'sourceCode = 'console.log("Hello, World!");'connectCommonJsAmdtoCommonJssourceCode;
You can use this module's
toCommonJs function, in conjunction with the
var express = require'express'path = require'path'connectCommonJsAmd = require'connect-commonjs-amd'port = processargv2 || 3000publicFolder = pathjoin__dirname 'public'server = expresssrcFolder = pathjoin__dirname 'src';serverconfigureserveruseexpressbodyParser;serveruseexpressmethodOverride;// Convert all your CommonJS CoffeeScript to AMD.serveruserequire'connect-coffee-script'src: srcFolderdest: publicFolderoptionsbare = true;code = CoffeeScriptcompilestr options;// Here's the call to `toCommonJs`.return connectCommonJsAmdtoCommonJscode;;serveruseexpress"static"publicFolder;serveruseexpresserrorHandlerdumpException: trueshowStack: true;serveruseserverrouter;;serverlistenport;console.log"Server listening on port " + port;
Mocha is used for the testing suite. And, you can run the test using the following command.
$ npm test
Or you can use the
mocha command. Both the same thing.