Full Node.js demo REST API for 'Subdelegaciones IMSS'. New exercise based on "Let's Build a Node.js REST API" blog serie from Victoria Lo and the exercise on freeCodeCamp site from Nishant Kumar (links below).
This project will be operated by workers of the Coordinación de Afiliación - División de Soporte a los Procesos de Afiliación, an office on Instituto Mexicano del Seguro Social (IMSS, DIR, CA, DSPA) from Ciudad de México, México.
This project must build a RESTful API using Node, Express and MongoDB with the help of this awesome tutorial series Let's Build a Node.js REST API made by Victoria Lo
Also, we build this project with the help of this page How to build a RESTful API using Node, Express and MongoDB made by Nishant Kumar
With this API, users can get general info about all the 'Subdelegaciones IMSS' from México. Subdelegaciones are the customer's office for a variety of formalities ono social security.
git clone git@github.com:FernandoTorresL/SAPI.git
Optional: You can change <my_folder> on this instruction to create a new folder
git clone git@github.com:FernandoTorresL/SAPI.git <my_folder>
Create your own .env file based on .env.example file on main folder with connection string to MongoDB and optional Port number.
Replace the values:
- <your_user>, This is the database user defined on MongoDB
- <your_password>, the password defined for your user
- <name_cluster_mongodb>, the name of the Cluster on MongoDB
- <collection_name>, the name of the Collection
- <port_number>, the port number to use
Confirm values on your MongoDB Database and Cluster page
DATABASE_URL=mongodb+srv://<your_user>:<your_password>@<name_cluster_mongodb>/<collection_name>?retryWrites=true&w=majority
PORT=<port_number>
For example:
DATABASE_URL=mongodb+srv://fertorresmx:12345678@cluster1.ABC.mongodb.net/Subdelegaciones?retryWrites=true&w=majority
PORT=3000
$ cd SAPI
$ npm install
$
$ npm start
$
$
Example output
$ > sapi@1.0.0 start
$ > nodemon index.js
$
$ [nodemon] 3.0.1
$ [nodemon] to restart at any time, enter `rs`
$ [nodemon] watching path(s): *.*
$ [nodemon] watching extensions: js,mjs,cjs,json
$ [nodemon] starting `node index.js`
$ App started and listening on port 3000
$ Database connected!
This prompt may vary if you use another shell configuration or operating system, like pk10, git bash, WSL, Linux...
Now, after check that "Database connected!" message is on your terminal, you can test your routes using tools like Postman.
For example, to test this method use this values on Postman (check image below):
- Method: GET
- URL: localhost:<port_number>/api/sub/getAll
- Params:none, Body:none
- Response: a JSON with all the values on the collection
-
Method: POST
-
URL: localhost:<port_number>/api/sub/post
-
Body type: Raw, JSON
-
Body content: A JSON with a valid schema
Example:
{
"name": "Chilpancingo",
"subdelegacion_id": 15,
"delegacion_id": 21,
"old_address": "Domicilio conocido",
"num_sub": 50,
"status": true
}
- Response:
"name": "Chilpancingo",
"subdelegacion_id": 15,
"delegacion_id": 21,
"old_address": "Domicilio conocido",
"num_sub": 50,
"status": true,
"__v": 0
}
- Method: GET
- URL: localhost:<port_number>/api/sub/getOne/id
- Params:none, Body:none
- Response: a JSON with the document data with that id
- Method: PATCH
- URL: localhost:<port_number>/api/sub/update/id
Example: localhost:3000/api/sub/update/6554238f791d0509e5795g9c
-
Body type: Raw, JSON
-
Body content: A JSON with a valid schema
Example:
{
"name": "Salina",
"subdelegacion_id": 10,
"delegacion_id": 200,
"old_address": "NA"
}
- Response (only the indicated data on body will change):
"_id": "6554238f791d0509e5795g9c",
"name": "Salina",
"subdelegacion_id": 10,
"delegacion_id": 200,
"old_address": "NA",
"num_sub": 1,
"status": true,
"__v": 0
}
- Method: DELETE
- URL: localhost:<port_number>/api/sub/delete/id
Example: localhost:3000/api/sub/delete/6554238f791d0509e5795a9c
- Params:none, Body:none
- Response:
Document with sub tmp has been deleted..
We use this guide to deploy to Heroku
- Fork it (https://github.com/FernandoTorresL/SIAP/fork)
- Create your feature branch (
git checkout -b feature/foo_bar
) - Commit your changes (
git commit -am 'Add some foo_bar'
) - Push to the branch (
git push origin feature/foo_bar
) - Create a new Pull Request