Backend generator for Yeoman with Hapi.js and other cool stuff :)
To start using this generator you need to clone or download it, and link it into the node modules folder of your system. To do this, you need to execute this command into the generator-back project folder:
npm link
After linking the generator, we need to create the project folder for our new REST API, and execute inside this new folder:
yo back
There are a few configuration questions and then a npm package installation. After this, the API is ready to be used. Just launch a local MongoDB server, Redis server and our new app (I assume you have installed MongoDB and Redis):
mongod
redis-server
nodemon server.js
To test our API y use Postman. It's easy, it's cool and it's free. Good for me. The steps you can follow are:
To create a new user, you need to do a POST with an object with the following data:
http://localhost:5000/signup
{
"name": "My Name",
"surname": "My Surname",
"username": "my_username",
"email": "my@email.com",
"password": "123456",
}
To login to the system, you need to use a GET with the username and password in the query of the call:
http://localhost:5000/login?username=my_username&password=123456
What you get in response is an object with a token. This is your session token and you need it for every protected communication with the server. The password is only send to the server in the login, and you can also hash it using a md5 to even keep it in the server (I try never to store password in the server, just in case).
{
"token": <token value>
}
Because we are going to make a lot of secured calls, we are going to use the request header to store the username and the token. The token is the one returned by the login, so use it here.
http://localhost:5000/users
username -> my_username
token -> <token value>
Be careful because we just add the user but here we don't check if the username already exists. We just do that in the register.
http://localhost:5000/users
username -> my_username
token -> <token value>
{
"name": "Your Name",
"surname": "Your Surname",
"username": "your_username",
"email": "your@email.com",
"password": "123456",
}
The same as get all the user but passing the user username
. This is because the parameter we pass on the query call is optional ({username?}
), so we decide what we return whether if exists or not.
http://localhost:5000/users/your_username
username -> my_username
token -> <token value>
Just change the data in the new user.
http://localhost:5000/users/your_username
username -> my_username
token -> <token value>
{
"name": "His Name",
"surname": "His Surname",
"username": "your_username",
"email": "his@email.com",
"password": "12345678",
}
And finally get rid of him.
http://localhost:5000/users/your_username
username -> my_username
token -> <token value>
The modules we have used here are the following:
- hapi : This is the wonderful tool to create the REST routing.
- good : This is a logger tool to see the server log in the terminal.
- joi : A tool to verify the fields passed to the route.
- bcrypt : To make the encryption to store the password in the database and to check it when login.
- mongoose : Our ODM for MongoDB.
- redis : The Redis connection for Node.js.
- hiredis : The Redis tools.
When we launch the test client siotest.js
that use socket.io-client
, we create a new websocket with the server and to test it we just need to use two services that emit two messages to the client that responds and match two listeners in the server:
Just launch the server and the test client, and use the explorer to visit the two addresses.
Under construction...
MIT
Hope you find it useful :)