Alright, so we'll use the http library to create a basic server. Basic components will look like this:
src/
controllers/
server.js
router.js
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:
/ returns text/html and starting html for the site.
static/{filename} returns a static file - js, jpg, png, css, etc.
api/ is the api root
title/ returns all Titles available - tmdbid, title
add ?fields= to get more fields than just id and title.
add ?filter= to filter titles by a TMDB field
add ?search= to search by title
title/discover/ aliases TMDB's discover/movie and discover/tv. pass ?params= as a list of parameters to pass off to TMDB. This endpoint also supports ?fields=. TMDB has a genre/ endpoint, but I think we can get that info through discover anyway. If not, we can optimize that on the backend and pretend it fits here.
title/[latest|upcoming|popular|top_rated] aliases the corresponding TMDB endpoints on movie.
title/{TMDBId}/ returns data for a particular title. It's basically just a proxy for getting data from TMDB with our private api key. It'll always return tmdbid, instances and title, as well as any other fields requested in a comma-separated fields query string key, e.g., ?fields=description,genres. Some of these fields aren't fields in the TMDB api, but they are endpoints, so we'll have to handle that backend, for example: rating and reviews. The instances key is a list of discrete physical videos, as they show up in the movie/{hhid} endpoint.
title/{TMDBId}/videos/ will use the movie/{id}/videos TMDB api endpoint and return an array of video urls.
title/{TMDBId}/similar/ will use the movie/{id}/similar TMDB api endpoint to return a list of similar titles.
movie/{hhid}/ returns a single HH movie instance with fields hhid, tmdbId, checked_out, rating, reviews
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 keysuccess
, and if there is data, it should be nested under adata
key. Error responses should always have the keyerrors
, 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:
/
returnstext/html
and starting html for the site.static/{filename}
returns a static file - js, jpg, png, css, etc.api/
is the api roottitle/
returns all Titles available -tmdbid
,title
?fields=
to get more fields than just id and title.?filter=
to filter titles by a TMDB field?search=
to search by titletitle/discover/
aliases TMDB'sdiscover/movie
anddiscover/tv
. pass?params=
as a list of parameters to pass off to TMDB. This endpoint also supports?fields=
. TMDB has agenre/
endpoint, but I think we can get that info through discover anyway. If not, we can optimize that on the backend and pretend it fits here.title/[latest|upcoming|popular|top_rated]
aliases the corresponding TMDB endpoints onmovie
.title/{TMDBId}/
returns data for a particular title. It's basically just a proxy for getting data from TMDB with our private api key. It'll always returntmdbid
,instances
andtitle
, as well as any other fields requested in a comma-separatedfields
query string key, e.g.,?fields=description,genres
. Some of these fields aren't fields in the TMDB api, but they are endpoints, so we'll have to handle that backend, for example: rating and reviews. Theinstances
key is a list of discrete physical videos, as they show up in themovie/{hhid}
endpoint.title/{TMDBId}/videos/
will use themovie/{id}/videos
TMDB api endpoint and return an array of video urls.title/{TMDBId}/similar/
will use themovie/{id}/similar
TMDB api endpoint to return a list of similar titles.movie/{hhid}/
returns a single HH movie instance with fieldshhid
,tmdbId
,checked_out
,rating
,reviews