Skip to content

hrkbrahmbhatt/Assignment4

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Assignment4

Profile management: Node.js and Express

Environment vars

This project uses the following environment variables:

Name Description Default Value
CORS Cors accepted values "*"  

Pre-requisites

  • Install Node.js version 16.17.0
  • Install POSTMAN

Getting started

  • Clone the repository
git clone  <git lab template url> <project_name>
  • Install dependencies
cd <project_name>
npm install
  • Build and run the project
node app.js

Navigate to http://localhost:3000 in POSTMAN

Express + Node

The main purpose of this repository is to show a project setup and workflow for writing get, post and update data and perform write file .

Project Structure

The folder structure of this app is explained below:

Name Description
node_modules Contains all npm dependencies
controllers Controllers define functions to serve various express routes.
controllers/user.js Handling the data coming from route
routes Contain all express routes, separated by module/area of application
src/models/data Models define schemas that will be used in storing and retrieving data from App database
data/post.json storing added and updated data
/app.js Entry point to express app
package.json Contains npm dependencies as well as build scripts
tslint.json Config settings for TSLint code style checking

-- The endpoints and their functionalities that you need to create are as follows: -- 1) /add -> POST Method -> This route should add the data sent with the request body into the post.json file. -- Sample data sent with the request:

    "id": "1",
    "name": "updated name",
    "age": "22",
    "gender": "Male",
    "email": "userone@gmail.com"
}
  • If the data is added successfully, then you should sent a response code of 200.

-- Output Here's what it looks like in action. ( Wait for GIF )

Alt Text

    1. /view -> GET Method -> This route has a optional query param called id. If no query param get passed with the request then you should fetch all the data from the get.json file. If some id gets passed with the request query param, then you should send the details0 of the user having id equal to the id that comes with the request query.
  • Sample request without query parameter:

  • /view

  • Expected response:

  [ {
    "id": "1",
    "name": "updated name",
    "age": "22",
    "gender": "Male",
    "email": "userone@gmail.com"
  },
  {
    "id": "2",
    "name": "user two",
    "age": "24",
    "gender": "Female",
    "email": "usertwo@gmail.com"
  },
  {
    "id": "3",
    "name": "user three",
    "age": "23",
    "gender": "Male",
    "email": "userthree@gmail.com"
  } ]

-- Output Here's what it looks like in action. ( Wait for GIF )

Alt Text

  • Sample request with query parameter id:
  • /view?id=2
  • Expected response:
      "id": "2",
      "name": "user two",
      "age": "24",
      "gender": "Female",
      "email": "usertwo@gmail.com"
      }]
  • If the data is fetched successfully, then you should send a response code of 200.

  • Output Here's what it looks like in action. ( Wait for GIF )

Alt Text

    1. /edit/:id -> PATCH Method -> This route should update the values of the of the user having id in post.json equal to the id that comes with the request URL. You can update name, age and email of a user.
  • Sample request:
  • /edit/3
  • Sample data sent with the request:
    "name": "updated name"
}

Output Here's what it looks like in action. ( Wait for GIF )

Alt Text

This request should update the name of the user having id 3 in post.json as updated name. If the data is updated successfully, then you should send a status code of 200.

Common Issues

npm install fails

The current solution has an example for using a private npm repository. if you want to use the public npm repository, remove the .npmrc file.

About

Express + Node to get, post and modify user data

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors