4 ms·
Got a question to y'all regarding the whole node.js stuff. I downloaded ExpressJS framework and tried to create a simple MVC apps a'la Rails... I get the impr
by hello_moto 15y ago
Got a question to y'all regarding the whole node.js stuff.
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.
- pkrumins 15y agoYou can structure your code any way you wish. There are no rules on how you should write your webapp. 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.
- hello_moto 15y agoThanks for the info. Next question for you (since I know you use a lot of Node.js). What's the recommended approaches for unit-testing your Node apps (Socket.IO, ExpressJS, whichever)? 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?)
- pkrumins 15y agoAbout unit testing - I like expresso [1], but recently also started using tap [2]. [1]: https://github.com/visionmedia/expresso https://github.com/visionmedia/expresso [2]: https://github.com/isaacs/node-tap https://github.com/isaacs/node-tap Also see my startup that does javascript unit testing in all the browsers: http://www.testling.com http://www.testling.com Now about switching environments, see this: http://expressjs.com/guide.html#configuration http://expressjs.com/guide.html#configuration And also see bouncy to route between production/development dynamically: https://github.com/substack/bouncy https://github.com/substack/bouncy
- funthree 15y agoThere are a number of ways to do this but when I first started with node I started by using some different files which hold environment variables. You can then export some environment variables based on where you are running your code (production, dev, staging, etc) 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 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.
- kainosnoema 15y agoNo. Keeping all your code in one file is a recipe for maintainability disaster. I've found that with Javascript, proper project organization is even more important than with some other languages. 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 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 https://github.com/visionmedia/express/tree/master/examples
- chapel 15y agoExpress is very flexible, and can work in just about any way you could imagine. Take no offense but it sounds like you aren't very familiar with javascript or node.js and that is okay, but there is much to learn about project organization. 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/mvc 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.
- Vitaly 15y agoThis is one of the things that Rails gets right. There is no real need to be creative here. Rails just comes with a default layout built in, no need to 'choose your own' for the 'own' sake. It doesn't, btw, mean that you can't use your own, just in this case you'll need to add the relevant directories to the path yourself. The standard layout and built in generators are just brilliant.
- chapel 15y agoOf course it is a matter of opinion, but you have to keep in mind that node itself isn't a framework like rails, and express while similar to sinatra with declaring routes is very un-opinionated about how it handles things. That is one reason I enjoy using node and a lot of the modules and add-ons created for node, giving me the freedom to do what I want from the start and not fight against the 'right' way the creators intended.
- Vitaly 15y agothis is a common misconception about rails. 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.
- tjholowaychuk 15y agoyou just need to learn how node modules work, this is a big issue for a lot of people but it's the result of node's module system, not express