suprmat95 / nodejs_https_apis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

nodejs_https_apis_exercise

Dependencies

  1. Install Node.js and npm:
    $ curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash - && sudo apt install -y nodejs
  2. Install MongoDB, following this link MongoDB
  3. Install [project]'s dependencies, in the project folder type:
    $ npm i -s

Launch the Server

  1. Start MongoDB:
    $ sudo service mongod start    
  2. Optionally, free the server port which is set to 3000 by default:
    $ lsof -i tcp:3000 | grep LISTEN | awk '{print $2}' | xargs kill
  3. Run the server:
    $ chmod u+x server.sh
    $ ./server.sh
  4. Optionally, it's possible to chose a port with the command:
    $ PORT = typeyourport ./server.sh

APIs

Following a brief explanation of the available APIs which can be tested simply using software like postman. The body must be written in JSON format.

  1. Users APIs:

    1. POST to create a new users:
         localhost:3000/users 
      Body:
         {"name": "name","password" : "password"} 
      Result:
      1. 200 OK
      2. 400 Request failed! Please check the request
      3. 403 The name is already present in the db
  2. Login APIs:

    1. GET to login and to get a valid Token:
         localhost:3000/auth/login 
      Body:
         {"name": "name","password" : "password"} 
      Result:
      1. 200 OK
      2. 400 Authentication failed! Please check the request format
      3. 403 Username not present in db or Password doesn t match! Please check the request
  3. Resource APIs:

    All APIs need authentication, after logging, copy the token obtained in the Bearer Token field.

    1. POST to create a new resource:

         localhost:3000/auth/resource 

      Body:

         {"id": "idResource", "data": [{"field1": "example"}]}

      The data field is the only one required. Within the data field, up to 10 fields with the name from field1 to fiel10 can be specified. If the id field is not present in the request body will be generated by the server.

      Result:

      1. 200 OK
      2. 400 Resource data can not be empty
      3. 500 Some error occurred while creating the Resource
    2. GET to obtain all resources:

         localhost:3000/auth/resource 

      Result:

      1. 200 OK
      2. 500 Some error occurred while finding the Resources
    3. GET to obtain one resources.

         localhost:3000/auth/resource/idResource

      Result:

      1. 200 OK
      2. 500 Error retrieving resource with id
      3. 404 Resource not found with id
    4. PUT to update one resources:

         localhost:3000/auth/resource/idResource

      Body:

         {"data": [{"field1": "newValue"}]}

      A resource that has been previously deleted cannot be updated. Result:

      1. 200 OK
      2. 500 Error updating resource with id
      3. 404 Resource not found with id or already deleted
      4. 400 Resource data can not be empty
    5. DELETE to delete one resources:

         localhost:3000/auth/resource/idResource

      Result:

      1. 200 OK
      2. 500 Error deleting resource with id
      3. 404 Resource not found with id or already deleted

Launch the Tests

  1. Run the tests:
    $ npm run test

About


Languages

Language:JavaScript 99.9%Language:Shell 0.1%