Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

NodeJS Assignments

Assignment - 1
Write code to get data using different methods like process.argv in node command itself, as env
variable, and using the readline to have interactive terminal session

1. function getNameFromCommandLine() {
// Write you code here, name should be taken as args in process.argv
}
2. function getNameFromEnv() {
// Write your code here
}
3. function getNameFromReadLine() {
// Write your code here
}
Instructions
* In the first function, you would receive name as the last command line argument, you need to
return the name from the function
* In this second function, return the env variable name
* In the last function, accept the name from the command line (terminal) using readline in nodejs

Assignment - 2
● Create a file and serve it using simple http server
● Write code to create a simple file containing the dummy html code provided below using
the fs module, serve the file created using the http server
Instructions
● Create a file called index.html in the same folder using the nodejs fs module before
starting the server
● The index.html file should contain <h1>Hello World</h1>
● Serve the index.html file using nodejs http module
● Server should listen on port 3000

Assignment - 3
# Create a simple nodejs app using express

## Create a nodejs server using express, ejs as view engine, and any other dependencies
required

Instructions
* Create an express server with the routes mentioned below
```
GET / - Base Route, return the homepage or the landing page
GET /form - should render a form
POST /user/add - Should add a user and redirect to base route '/'
```

* The main file for you app should be called app.js


* Store the ejs files inside views folder, create a form.ejs file to render the form for adding user
* Initially create users array (array of user object) with some dummy users present, user object
has name, email keys and their corresponding values
* The base route '/' should display a list/card with the users (their name and email), this is the
homepage or the landing page of your app
* There should be add add button on the homepage which would redirect to '/form' i.e the form
page to add new user
* The form route should redirect to a page where a form with inputs for name and email are
shown with a submit button that should make a post request to /user/add with the data inserted
in the input fields
* On receiving the request to add user i.e on /user/add route, you should add the user to the
users array and redirect to base route '/'
* Now you should be able to see the new user added on you screen

Assignment - 4
Extension to Assignment - 3
Add PUT and DELETE routes in previous assignments
This is an extension on assignment 3, now use a database to store the users, and create routes
to edit and delete a user

Instructions

Create a dataase connection to mongodb on your localhost with the database name as
assignment_4

Create a user schema with name, email (unique), and a boolean field called isPromoted, default
value null

The two addtional routes are added below

PUT /user/:id - Edit User Route, toggle the isPromoted field value to true / false
DELETE /user/:id - Delete the user with the id
The user list / card in the base route should show two buttons (Promote / Demote, and Delete)
in every user list / card
Add the buttons in the format below

<form method="POST" action="/users/<%= user._id %>?_method=PUT">


<button id="edit-<%= user.name %>" type="submit">Select</button>
</form>
<form method="POST" action="/users/<%= user._id %>?_method=DELETE">
<button id="delete-<%= user.name %>">Delete</button>
</form>
Use npm method-override package to override POST to PUT/DELETE method on backend

The Promote / Demote button should call the PUT request with the user id for that user and
should change the user isPrmoted field to true (if it was null or false) else false
Show the users with isPromoted value as null in Yellow, isPrmoted true value in Green,
isPromoted false value in Red
Add one more button called Delete on every user list / card and on clicking the Delete button, it
should call the DELETE routee with the user id and the user should get deleted on the backend
Give the edit button and delete button their unique ids in format (edit-) and (delete-) for their
corresponding users
Redirect to the base route after PUT and DELETE route

Assignment - 5
Create a REST API backend service using NodeJs and Express
Create a backend service for a post sharing application like instagram, the user should be able
to add posts, edit and delete them as required

Instructions

Create an express server that listens for request on Port 3000


Create user and post schema with { name, email, password } for user and { title, body, image,
user } for post
Create login and registration API for users to log into the app and register a new user, store the
data in the mongodb
Register New User
POST - /register - Accepts name, email and password in the body, the email Id should be
unique i.e no two users can have the same email id
POST - /login - Accepts email and password in request body, verify the email and
password and respond with correct http status code in case of success or failure
Return token in the response after successful login (Example Response - { token: <JWT
Token> } )
Add CRUD routes for Posts create, read, update, and delete operations
GET - /posts - Get all the posts in the db, the response should be in format
(Example - { posts: [] } )
POST - /posts - Create a new post (accept title, body, image) and store the info in
the db with the user reference, return the post created in the response (Example - { _id, name,
title, body, user } )
PUT - /posts/:postId - Edit/Update a Post with the id provided
DELETE - /posts/:postId - Delete a Post with the id provided
Add a middleware to perform authentication and authorization for users (only logged in users
can access the posts apis - use JWT for authentication)
Only authorized users can perform Edit/Delete Operation for the posts, userA should not be
able to edit/delete userB's posts
Use appropriate status code for different operations
Example - Use 200 http status for successful request, use 4xx status code for unauthorized,
forbidden response

You might also like