A Simple MVC Setup In Node.JS

On February 20, 2011, in Web Coding, by admin

I’m sure many of you have heard about the new server side javascript project called node.js. I’ve been using myself to build full stack dynamic websites and find it a very nice environment to create a dynamic website. When I first started out I had no idea what sort of way one should create an MVC style framework with node or what modules would help me to achieve the model, view, controller ideal. Heres my stack:

  • Node.js (http://nodejs.org/) of course
  • NPM (http://npmjs.org/) to easily install packages for node.
  • Express (http://expressjs.com/) – a very nice web development framework built on connect (allows “controllers”)
  • Mongoose (http://mongoosejs.com/) – a solid orm for mongodb in node.js that just released a much improved version (allows “models”)
  • Jade (http://jade-lang.com/) – a template engine for node that allows some cool shorthand (allows “views”)
  • Other tools were used of course but those were the essentials.

The application directory looks something like this:

1
2
3
4
5
6
7
8
9
10
/app-root
  /models
  /views
  /controllers
  /public
  /tests
  /scripts
  /logs
  /tmp
  app.js

Inside the application I instantiate express, include my mongoose models and include my routes/controllers from the controllers directory at the bottom. Inside the controllers I call res.render to render the jade views and it all works very nicely. If people would like me to elaborate on this more let me know.

A huge shoutout and thank you to the LearnBoost guys (TJ Holowaychuk, Guillermo Rauch, Aaron Heckmann, and many more that aren’t associated with LearnBoost) who’ve made Node.js web application development a breeze.

Be Sociable, Share!
 

22 Responses to A Simple MVC Setup In Node.JS

  1. I am definitely interested in seeing more. Perhaps a small screencast, or a tutorial on how to set this all up? A base “Controller” class that handles all the regular things you see in an MVC framework would be nice too.

  2. admin says:

    Hmm … if I get some free time maybe I’ll put together a small tutorial with screenshots etc.

  3. Evan says:

    I’d love to see a sample app.

  4. Giovanni says:

    Can you post a simple app?

  5. admin says:

    Hey guys, I can see theres a clear demand for a solid example app in node. I’ll throw a solid one together and post it later this week, thanks for the interest!

  6. MaxGfeller says:

    You can take a look at my project mongee, it uses some kind of mvc patterns but without the views. http://www.github.com/MaxGfeller/mongee

  7. dag says:

    Thank you a lot! Sample app would be great.

  8. gulbrandr says:

    good article. Can be even better with a sample application..
    thanks.

  9. john says:

    Travis! Your logo for Exipe is identical to the trademarked logo for the highly popular linux distribution, Arch Linux.

  10. admin says:

    Wow, I’ll say. Thank you for the heads up, we’ll have to look into changing it or something.

  11. mick says:

    great post! I’m come from Ruby on Rails, but now I’m appreciating node.js too ;-)

  12. Mason says:

    Awesome! Would also like to see some more detail on your setup :)

  13. johans says:

    You probably have Stylus on your list under “other tools” – really nice for CSS:
    http://learnboost.github.com/stylus/

  14. johans says:

    For those looking how to set this up you can follow the “Lets make a web app” tutorial on DailyJS that uses all these components: http://dailyjs.com/tags.html#lmawa

  15. Clifton says:

    I’ve just put a package into npm (express-mvc-bootstrap), that will create a template application very similar to this structure, but with the addition of scripts that can create the M, V and C from templates.

    http://cliftonc.github.com/express-mvc-bootstrap/. Some of the ideas from this blog (and of course all the learnboost stuff that is fantastic) were all inputs.

    Comments welcome!

  16. Steve says:

    Have you posted sample code of the basic setup? It would be very helpful to see the basic server code, etc. Thanks for all your work on this!

  17. john casin says:

    its really great
    really thanks :)

  18. Dan Smart says:

    Have you tried Monorail.js (https://github.com/runexec/Monorail.js) ?

  19. abhinav says:

    Thanks a lot! for sharing :)

  20. site says:

    Fantastic Stuff, do you have a facebook profile?

  21. Aaron says:

    I would love to see more on this. I’m thinking of using node.js and your mvc based setup interests me.

  22. Diego Romero says:

    Thanks, any actual example on node performance testing? thanks again

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>