Install Node.JS
and npm
:
sudo apt install nodejs
node -v
npm -v
npm
is a package manager for Javascript that also keeps track of the packages in your projects.
Then install the following NodeJS packages:
express
- our web application frameworkbody-parser
- parsesContent-Type
s in Requestjsonwebtoken
- our JWT token managermongodb
- our databasemongoose
- communicates with our MongoDB databasemorgan
- logs our middleware
npm express body-parser jsonwebtoken mongodb mongoose morgan --save
--save
just makes sure our packages are saved in ourpackage.json
file
Then install nodemon
which is a library that helps our server reload everytime a file is changed and saved. Without nodemon
we'd have to shut down the server and restart it:
npm install --save-dev nodemon
--save-dev
savesnodemon
as a development dependecy
Run:
nodemon server.js
POST
/api/authenticate
Request:
{
"email":"[email protected]",
"password":"password"
}
Response;
{
"id": "5beca3a6d118642f620fe04b",
"email": "[email protected]",
"first_name": "Nick",
"last_name": "Cerminara",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZG1pbiI6dHJ1ZSwiaWF0IjoxNTQyMjM3MTY2LCJleHAiOjE1NDIzMjM1NjZ9.96a4g7Btd6DCgBzt4Ysx6Hu5y60nBMu8fYiQ0pJ1LnI"
}
Status Codes:
- 200 - OK - If authentication was validated
- 404 - Not Found - If user is not found
- 403 - Forbidden - Wrong password
This endpoint return a list of all the users in the database. This happens only because my 'dummy' user is and admin
.
GET
/api/users
Request:
A request can be made different ways:
Header:
x-access-token
Parameter on the url request:
localhost:8080/api/user?token=2394829384
Or an application/json
body
{
"token":"akdh9384wherofa"
}
Response:
[
{
"id": "5beca3a6d118642f620fe04b",
"email": "[email protected]",
"first_name": "Nick",
"last_name": "Cerminara"
}
]
Status Codes:
- 200 - OK - If authentication was validated
- 403 - Forbidden - No token given