Raffaeu
Raffaeu

Reputation: 6973

Node.js how to structure an HTTP REST APi project?

I am going to learn Node.js and right now I was trying to make a small demo project which implements an HTTP REST service. My idea is to divide the APIs (url) by Resources and end up in a structure like the following one:

- user
 > index.js
 > post.js
 > put.js
 > delete.js
 > functions.js
- person
 > index.js
- index.js

So, if you are familiar with ASP.NET Web Api, I am trying to make every module of Node.js a Controller and every web method a single file (.js), in order to have an high maintainability in the future.

Question Right now, my index.js file return the following:

var http = require('http');
var server = http.createServer();

How can I configure a specific "Request Handler" in each file by using this module? At the moment the createServer() method return a server object can use a single server.on('request', function) while I need to handle each request in a different file.

Upvotes: 2

Views: 3729

Answers (1)

XCEPTION
XCEPTION

Reputation: 1753

Go ahead learning with the help of some framework. They provide scaffolding of project.

If you are developing a complete web app(MVC) then go for ExpressJs or SailsJs

If you are looking out to develop only API(No Views) then go for Strongloop or Restify

There are many more frameworks but the above ones are popular.

Upvotes: 2

Related Questions