Node.js Express for REST? Are there controllers in Express?
Asked Answered
A

3

6

I'm currently developing a community (like a lightweight "social network") for a limited (local) target. This is the first project where SEO doesn't matter and where I can happily exclude no-js users. That's why I'm thinking to start the project over and write my first site that is completely build with Javascript and my first Node application for educational reasons.

Details so far:

Browser: jQuery, maybe JavaScriptMVC (there are some things I don't like about JavaScriptMVC (like the routes), maybe I write my own little MVC or do you know a better suited framework?)

Server: Node.JS, Express framework, (maybe socket.io or nowjs for further features)

I got a few questions so far. I know it's better to ask a single question but there are more or less connected:

  • Express looks really nice but I'm missing MVC. I couldn't find any project that implements mvc and is build on Express. Is there a reason for that? Routing is nice in Express but I need a way to spread code across multiple files (controllers would be the best way I guess, the application won't be small and I need it maintainable)

  • The application will be more or less completely based on AJAX (json) requests. Is Express the right framework for such applications, anyway? I think the best way to write this project is to expose a json REST api which can then be queried by the web application over AJAX and by a mobile device app (which I'm also going to write). In my opinion Express' route system is quite suited for REST. But feel free to recommend other frameworks.

Ayana answered 16/10, 2011 at 13:41 Comment(1)
For RESTful apps with express, I just started to play with github.com/visionmedia/express-resource . Looks promising.Balk
F
4

To answer your primary question, yes there are controllers in Express. They are more akin to Sinatra (express is modeled after Sinatra rather than Rails). By that I mean they are light-weight method mappings to routes you define.

Browser: jQuery, maybe JavaScriptMVC (there are some things I don't like about JavaScriptMVC (like the routes), maybe I write my own little MVC or do you know a better suited framework?

I'd go with spine.js or backbone.js personally.

Express looks really nice but I'm missing MVC. I couldn't find any project that implements mvc and is build on Express. Is there a reason for that? Routing is nice in Express but I need a way to spread code across multiple files (controllers would be the best way I guess, the application won't be small and I need it maintainable)

You can spread files out all you want. I answered this here.

The application will be more or less completely based on AJAX (json) requests. Is Express the right framework for such applications, anyway? I think the best way to write this project is to expose a json REST api which can then be queried by the web application over AJAX and by a mobile device app (which I'm also going to write). In my opinion Express' route system is quite suited for REST. But feel free to recommend other frameworks.

I haven't built a 1 page app yet in node but from what I can tell, almost everyone seems to be using socket.io with backbone. That's not to say you can't, just that you'll find more examples that way.

Faythe answered 16/10, 2011 at 13:55 Comment(1)
I don't know if socket.io is too heavy for just showing sites. However it'd be nice to broadcast status messages in realtime and I think I'm going to implement a chat later, so I'd need it anyway. But then I still need REST or similar for the mobile application. Hmm, so many ways :D.Ayana
L
5

Have you seen Swagger? An API server available in Node.js with an automated UI Generator.

Swagger
(source: wordnik.com)

Laidlaw answered 21/1, 2012 at 21:49 Comment(0)
F
4

To answer your primary question, yes there are controllers in Express. They are more akin to Sinatra (express is modeled after Sinatra rather than Rails). By that I mean they are light-weight method mappings to routes you define.

Browser: jQuery, maybe JavaScriptMVC (there are some things I don't like about JavaScriptMVC (like the routes), maybe I write my own little MVC or do you know a better suited framework?

I'd go with spine.js or backbone.js personally.

Express looks really nice but I'm missing MVC. I couldn't find any project that implements mvc and is build on Express. Is there a reason for that? Routing is nice in Express but I need a way to spread code across multiple files (controllers would be the best way I guess, the application won't be small and I need it maintainable)

You can spread files out all you want. I answered this here.

The application will be more or less completely based on AJAX (json) requests. Is Express the right framework for such applications, anyway? I think the best way to write this project is to expose a json REST api which can then be queried by the web application over AJAX and by a mobile device app (which I'm also going to write). In my opinion Express' route system is quite suited for REST. But feel free to recommend other frameworks.

I haven't built a 1 page app yet in node but from what I can tell, almost everyone seems to be using socket.io with backbone. That's not to say you can't, just that you'll find more examples that way.

Faythe answered 16/10, 2011 at 13:55 Comment(1)
I don't know if socket.io is too heavy for just showing sites. However it'd be nice to broadcast status messages in realtime and I think I'm going to implement a chat later, so I'd need it anyway. But then I still need REST or similar for the mobile application. Hmm, so many ways :D.Ayana
A
0

You could check out Sails. Its structure is based on Rails.

Allegra answered 22/8, 2012 at 15:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.