NodeJS basic project
This is a NodeJS project bootstrapped with node
.
- Node.js 12.22.0 or later
- NPM or Yarn
- Nodemon, See below installation guide.
- MacOS, Windows (including WSL), and Linux are supported
npm install -g nodemon
First, install the dependencies for this project (Make sure you're in the project directory):
yarn
# or
npm install
Then, run the development server:
yarn start
# or
npm run start
Open http://localhost:4000 with your browser to see Server listening at http://localhost:4000
.
There are a set of tasks to complete related to a basic Node.js project.
- The project involves creating an API to manage a list of movies using Express and a JSON file.
- Those tasks include implementing routes for fetching all movies, getting a specific movie by ID, adding new movies, updating existing movies, and deleting movies.
- Additionally, you'll need to implement search functionality for movies based on their name, ratings, genre, and language.
Note: Keep in mind that we're not setting up a database for this exercise; we'll be working with a JSON file for simplicity. Take your time to understand the provided code and work on the tasks within the next 3 days. If you have any questions or need clarification, feel free to ask.
-
Create a route to handle a GET request to fetch all movies and send the movies JSON array as a response. Note: Returns an empty array if movies not found.
-
Create a route that accepts a movie ID as a parameter and returns the details of the specific movie with that ID. Note: It should return a single json object if movies found, otherwise returns empty object.
-
Create a route to handle a POST request to add a new movie to the "movies" array. Note:
- Validate Id and name fields - these must be unique. If a movie already exists with the provided id or name, then return "Movie with ${id}/${name} already exists."
- Validate other fields, if a field not found or for invalid data, return "Invalid field - ${fieldName}"
- Must have at least one genre.
- Rating must be between 1 to 10(inclusive)
- Apply auto coercion for number fields(if possible). {"year": "2016"} should be converted to {"year": 2016} but {"year" : "abc"} should return "Invalid field - year".
- Return the added movie and a success message: "The movie has been added"
-
Create a route to update the details of a specific movie by its ID using a PUT request. Note:
- Id field is not modifiable.
- All the other fields are modifiable.
- Before updating, validate all the fields as Task 3.
- If validation failed then return an error message: "Invalid field - ${field} to update!"
- Otherwise, return the updated movie and a success message: "The movie has been updated"
-
Create a route to delete a movie by its ID using a DELETE request. Note:
- If the movie not found then return an error message: "Movie with id ${id} not found!"
- Return the deleted movie and a success message: "The movie has been deleted".
-
Create a route to search movies based on their name. The route should accept a query parameter named "name" and return an array of movies that match the provided name.
-
Create a route to search movies based on their ratings. The route should accept a query parameter named "rating" and return an array of movies with ratings greater than or equal to the provided value.
-
Create a route to search movies based on their language. The route should accept a query parameter named "language" and return an array of movies that match the provided language.
-
Create a route to search movies based on their genres. The route should accept a query parameter named "genre" which can contain a single genre or a comma-separated list of genres. Return an array of movies that match at least one of the provided genres.
-
Test all the implemented routes using a tool like Postman to ensure they are working as expected.
All the data contains in the movies array. Here's what the schema looks like:
[
{
"id": number,
"name": string,
"genre": [string],
"rating": number,
"year": number,
"language": string
}
]