or CMDB for short
We'll use Express and Node to create a database and a way to control it.
However, and this is important to understand, we will not create a front end interface for it.
- Fork this repo, and then clone it.
- COMMIT AFTER EACH STEP, call each commit by the name of the step
- Learn express
- Understand how APIs work
- Understand how a REST interface works
- Competencies:
- Express.js
- APIs
- REST
- JSON
- go in the movie-database directory and run
npm init
to create a new project - answer whatever you like to the questions
- install express:
npm install --save express
- also, install nodemon, it will make things easier:
npm install --save-dev nodemon
- open the project in your IDE or editor
- edit
package.json
and add the following:// previous package.json stuff here... "scripts": { "test": "echo \"Error: no test specified\" && exit 1", // this should be there "dev": "nodemon index.js" }, // next package.json stuff here...
- create a file "index.js"
- create a file ".gitignore". In this file, write, at the top:
node_modules
. This will prevent thenode_modules
directory to be added to your git repo - push
- you're set!
- with express, create a server, and make it listen on a port of your choice (e.g,
3000
) - make it so this express server, when receiving an url, answers
ok
- test your server by running
npm run dev
- commit ("step 2")
- with Express, create a route such as, when the url
/test
is invoked, answers:{status:200, message:"ok"}
- with Express, create a route such as, when the url
/time
is invoked, answers with:{status:200, message:<TIME>}
, where<TIME>
is the current time in hours and seconds like so:14:20
- commit ("step 3")
- with Express, create a route such as, when the url
/hello/<ID>
is invoked, answers with:{status:200, message:"Hello, <ID>"}
, where<ID>
may be anything the user wanted to pass. The user may also not pass anything. - with Express, create a route such as, when the url
/search?s=<SEARCH>
is invoked, answers with{status:200, message:"ok", data:<SEARCH>}
if<SEARCH>
is provided, and{status:500, error:true, message:"you have to provide a search"}
if it is not. Be sure to set the http status to500
too. - commit ("step 4")
- With Express, create four routes:
/movies/create
,/movies/read
,/movies/update
, and/movies/delete
, where these routes can answer anything (we will change it later) - OPTIONAL: call your routes
movies/add
,movies/get
,/movies/edit
, and/movies/delete
, if you prefer. It doesn't matter - In your javascript file, create the array of movies:
const movies = [ { title: 'Jaws', year: 1975, rating: 8 }, { title: 'Avatar', year: 2009, rating: 7.8 }, { title: 'Brazil', year: 1985, rating: 8 }, { title: 'الإرهاب والكباب', year: 1992, rating: 6.2 } ]
- With Express, make it so that when the url
/movies/read
is requested, you answer with{status:200, data:<MOVIES> }
where<MOVIES>
is the list of movies - commit ("step 5")
- With express, make it so when the url
/movies/read/by-date
is requested, you answer with{status:200, data:<MOVIES>}
, where<MOVIES>
is the list of movies ORDERED BY DATE - With express, make it so when the url
/movies/read/by-rating
is requested, you answer with{status:200, data:<MOVIES>}
, where<MOVIES>
is the list of movies ORDERED BY RATING, where the highest rating is at the top. - With express, make it so when the url
/movies/read/by-title
is requested, you answer with{status:200, data:<MOVIES>}
, where<MOVIES>
is the list of movies ORDERED BY TITLE - commit ("step 6")
- With Express, make it so that when the url
/movies/read/id/<ID>
is requested, you answer with{status:200, data:<MOVIE>}
, where<MOVIE>
is the movie defined by the provided<ID>
. If the id doesn't exist, then the answer should be:{status:404, error:true, message:'the movie <ID> does not exist'}
. Don't forget to set an actual404
status code. - commit ("step 7")
- With Express, make it so that when the url
/movies/add?title=<TITLE>&year=<YEAR>&rating=<RATING>
, it:- creates a new movie in the form:
{title: <TITLE>, year: <YEAR>, rating: <RATING>}
- it adds this new movie to the
movies
array - answer with the new list of movies just like for
/movies/read
- creates a new movie in the form:
- Make it so if:
<TITLE>
is missing, or<YEAR>
is missing or<YEAR>
is not made of 4 digits or<YEAR>
is not a number- ...then you answer
{status:403, error:true, message:'you cannot create a movie without providing a title and a year'}
- But if
<RATING>
is missing, set a default rating of4
- commit ("step 8")
- With Express, make it so that when the url
movies/delete/<ID>
is requested, you delete the corresponding movie, and answer with the new list of movies, just like for/movies/read
. if the id does not exist, answer with{status:404, error:true, message:'the movie <ID> does not exist'}
- commit ("step 9")
- With Express, make it so that when the url
/movies/update/<ID>?title=<NEW_TITLE>
, the movie designed by<ID>
gets it's title changed to<NEW_TITLE>
. Return the modified array of movies. - With Express, make it so that when the url
/movies/update/<ID>?title=<NEW_TITLE>&rating=<NEW_RATING>
, the movie designed by<ID>
gets its rating changed to<NEW_RATING>
, and its title to<NEW_TITLE>
. If a user provides any oftitle
,rating
, oryear
, the movie should change to reflect those modifications. Fields that the user did not provide should not change. In the example here, theyear
of the movie should not change, as the user only providedtitle
andrating
. - commit ("step 10")
- change the urls to use
HTTP VERBS
(look it up. Google "rest APIs", and see "how to build REST APIs with Express") - commit ("step 11")
- Save the data in a mongoDB database. See this tutorial for help
- commit ("step 12")
- create a CRUD app but for users (same as the one for movies, but with a
users
array that takes objects with usernames and passwords) - only allow an authenticated used to modify or delete movies
- commit ("step 13")