Node.js modules you should know about: socket.io
catonmat.net
catonmat.net
I downloaded ExpressJS framework and tried to create a simple MVC apps a'la Rails...
I get the impression that ExpressJS framework works better if you put the whole thing in app.js. Like... the whole code.
I tried to create another file other than index.js inside routes folder (let's call it something.js) so I can instantiate some JS controller code in app.js vs putting everything in one file. That doesn't work at all, "something" is undefined.
It turned out that I have to either put everything in app.js or in routes/index.js in order for ExpressJS to work correctly.
The whole experienced was... unexpected.
I'm kind of used to Rails MVC. So please tell me: am I using ExpressJS the way it shouldn't be used?
UPDATE: Thanks for the answers guys. Kind of what I would have guessed to hear => use module. Which is probably fine. Module in CommonJS does have a bit of bookkeeping beforehand.
I did have a bit of a hunch that I needed to know more about CommonJS and proper modern JS practices.
For example, you can break up your app.js and routes.js.
app.js:
var express = require('express');
var app = express.createServer();
require('./routes')(app);
app.listen(3000);
routes.js: module.exports = function (app) {
app.get('/', function (req, res){
res.send('Hello World');
});
app.post('/foo', function (req, res) {
// ...
});
}
Or if it's a small app, you can just keep it all in app.js.What about switching between PROD, DEV, TEST environment like Rails? I found that feature is great since y'know... those environments will have different settings (I'm guessing the answer to this one is to roll your own kind of thing?)
[1]: https://github.com/visionmedia/expresso
[2]: https://github.com/isaacs/node-tap
Also see my startup that does javascript unit testing in all the browsers: http://www.testling.com
Now about switching environments, see this: http://expressjs.com/guide.html#configuration
And also see bouncy to route between production/development dynamically: https://github.com/substack/bouncy
For instance you might have a .production file
export NODE_ENV="production"
export NODE_PORT="80"
And a .development file export NODE_ENV="development"
export NODE_PORT="3000"
Before you run "node app" just link to your variables with "$ . .production" or "$ sh .production"Again there are many ways to accomplish this same thing but this way is pretty straightforward if you are familiar with linux.
EDIT: To add to the link to configuration pkrumins posted (http://expressjs.com/guide.html#configuration)
process.env will hold all of your environment variables, and express.js will read "NODE_ENV" internally. A combination of the above, and setting up corresponding internal configuration in your node.js app will provide for a very clean setup.
It sounds like you haven't become familiar with Node's CommonJS compatible module system. Try starting there: http://nodejs.org/docs/latest/api/modules.html
Once you understand the basics of modules and requiring, I'd take a look at the ExpressJS example apps for organization ideas: https://github.com/visionmedia/express/tree/master/examples
It is actually quite easy to organize your code similar to a rails mvc application, the key is being consistent.
If you want to see a working example of an mvc style express application, then look at the examples provided with express: https://github.com/visionmedia/express/tree/master/examples/...
Again, there are many ways to organize your applications, just look around at what other people have done, or use the examples, and you should be able to make yours handle just the same.
Rails doesn't "force" you to use their way. You don't have to call your table "people" if your model is called "person". It just that you have additional freedom not to configure anything if you are ok with the default configuration.
in many other frameworks you always have to configure everything, abut in rails you might just go with the defaults and skip the configuration altogether.
If express had code generators that would generate standard layout for models etc it wouldn't hurt anyones' ability to use their custom layout.
Is there a stripped down version of it, or some other wrapper library around websockets? I've been playing with just using the native stuff, but the different versions of API implementation are biting me a little.