- restify Restify Documentation. 2 restify-jwt-community for JSON web token
- restify-errors for error handling while using restify
- Mongo and Mongoose as the CRM
- bcrypt to hash user passwords so not to save them as plain text
- Greater working knowledge of building a RESTful API.
- Restify gained knowledge on how to use another framework based on express.
- More understanding of how to use JSON web tokens to authenticate a user and protecting routes from un-authorized users
- async / await I now understand more on how to use the javascript async / await with try{} catch{} blocks instead or the typical .then() .catch() when working with promises
- Deeper learning and usage of es6
I plan on using this api to create a Vuejs or Angular app to go on top of it and create a full working web application.
-
You will need postman installed on your machine or the chrome extension for it
-
Mongo Cloud Atlas account.
-
In the root directory of the project you will need to create a new config.js file.
-
Clone the repo: https://github.com/LjCraft12/restifyAPI.git
-
Either open the repo in your IDE or in your terminal and run:
npm i
This will install the the dependencies for the project
Aftter the installation finishes you can start the server with:
npm run dev
this will open a port on your machine at localhost:3000.
Avaliable routes are (routes are being used by postman there is no front facing web app):
-
GET localhost:3000/customers
-
Get localhost:3000/customers/id <- where id is the _id created by mongoose when a new customer is created and will return a single user.
-
POST localhost:3000/customers <- this route will allow you to create a new user (if you are a registered user).
-
Delete localhost:3000/customers/id <- Again the id is the _id created by mongoose this route will delete the customer from the DB.
-
POST localhost:3000/register <- This route will allow you to register a new user that can then make a new customer.
-
POST localhost:3000/auth <- Passing in any created user will return you user and their token
- Open up postman
- Set the request type to POST. the route should be
localhost:3000/register
and set the headers to:Content-Type
application/json
. - In the body tab set the type to
raw
you will also need to then create a new user in JSON format with an email and password (Both are strings). - After sending it off you can check your Mongo Cloud Atlas cluster collections for the newly created user.
Now that you have a user registered you can create a new customer.
- In post man set the request type to POST. The route should be
localhost:3000/customers
and set the headers to:Content-Type
application/json
. - You will then need to get the
users
token. You can do this by opening a new tab in postman point at the routelocalhost:3000/auth
and set the request type to POST. - Copy the token that has been retrieved and paste it in the POST request tab for creating a new customer. The
key
will need to be set toAuthorization
and the value is set to: jwt (don't forget the space between jwt and the token you paste in). . - In the body tab set the type to
raw
customers are also created in a json format with name, email, and the optional balance (name and email are both strings the balance is a type of number and if left blank the default balance will be 0). - You should then check you work with looking back into your atlas cluster to see if a new collection called customers was created.
Now that you have a customer in the DB its time to update. (All steps will require you to have a token already set refer to creating a customer if you need a new token and the header still needs to have a Content-Type
application/json
)
- In post man set the request type to PUT. The route should be
localhost:3000/customers/id
(again where id is the _id created by mongoose this can be found in the db or by retrieving the list of users). - You can either update the whole user or just a single part of the user such as just their name, email or password.
- Again you can check your Mongo Atlas cluster to ensure the update took.
Now that you have a user in the DB we can retrieve them from the db (You will need your Auth token)
- In Postman set the req type to GET and the route to
localhost:3000/customers
That's it you should have back a list of users in your DB
Now that you have too many users in your DB its time to delete them (As always you will need your token)