staab / js-allonge

Repository for collaboration on the project for the Javascript Allonge group 2014
MIT License
1 stars 3 forks source link

Design server side api #1

Open staab opened 9 years ago

staab commented 9 years ago

Alright, so we'll use the http library to create a basic server. Basic components will look like this:

server.js will set up a really basic server, and require and hand off the request to the router defined in router.js. Router.js will process the request, taking care of the special case "/" to return text/html by default. If something asks for json at "/", it'll return a json error message and a 4-- response code. We'll define the urls in a method on the router, and pass the request along a pipeline of middleware methods on the router - this will be a perfect place for promises. The router method that maps urls to controllers will pass the request along to the controller, defined in the controllers subdirectory.

Each controller will be responsible for returning an instance of http.ServerResponse (or maybe a promise) that further middleware methods on the router will validate and return to the client.

Response type, when not at "/" will always be application/json, and we'll use standard HTTP status codes. 2xx for success, 4xx for bad request. 00's will be most common, but there may be cases where a 302 redirect, etc. might be useful. Successful responses should always have a key success, and if there is data, it should be nested under a data key. Error responses should always have the key errors, which will correspond to an array of errors. Use the router to validate that these requirements are met by the controllers.

Endpoints are as follows: