Giter VIP home page Giter VIP logo

rest-api's Introduction

FirstApp

This is the first app with RESTful-API

Installation :

npm init
express --save=ejs
npm install --save sequelize
npm install --save sequelize-cli
sequelize init


npm install --save jsonwebtoken

npm install dotenv --save

Setting Database :

psql postgres
postgres# CREATE DATABASE "rest_api";

Resource How to markdown

  1. adam-p | fork to my repository
  2. workshopper | fork to my repository

Setting Database on Sequelize

Create Model User

    sequelize model:create --name User --attributes username:string,password:string

Database Migration (to create table on database)

    sequelize db:migrate

Setting route

routes HTTP Description
/api/signup POST Sign up with new user info
/api/signin POST Sign in while get an access token based on credentials
/api/users GET Get all the users info (admin only)
/api/users/:id GET Get a single user info (admin and authenticated user)
/api/users POST Create a user (admin only)
/api/users/:id DELETE Delete a user (admin only)
/api/users/:id PUT Update a user with new info (admin and authenticated user)

For Running the application Step by Step:

1st Step

npm start
using postman go to url : (POST) http://localhost:3000/api/signup 
fill on x-www-form-urlencoded
key & value
username : your username (e.g : pdvega)
password : your password (e.g : pdvega)
role : your role (e.g admin or user or other)
Congratulation, you've got new user account!

2nd Step

You can sign in now.
Using postman go to url : (POST) http://localhost:3000/api/signin 
fill on x-www-form-urlencoded
key & value
username : your username (e.g : pdvega)
password : your password (e.g : pdvega)
Got the token.

3rd Step

Using postman go to url : (GET) http://localhost:3000/api/users fill on Headers
key and value
token : your token when signin.
If your role as administrator, you can get all the data users.
If you are not, you will get notification that "You are not Administrator."

4th Step

Using postman go to url : (GET) http://localhost:3000/api/users/:id fill on Headers key and value
token : your token when signin.
As long as your role is not 'admin', you still can see your detail data user, with access   http://localhost:3000/api/users/:your_id.
If you are not an Administrator, you will get notification that "You are not authorized to see other account."
If your role as administrator, you can see all the data users detail by their id.

5th Step

Using postman go to url : (POST) http://localhost:3000/api/users fill on Headers key and value
token : your token when signin.
For Administrator Only.
Create User on x-www-form-urlencoded with key and value:
username : 
password : 
role : user

6th Step

Using postman go to url : (PUT) http://localhost:3000/api/users/id fill on Headers key and value
token : your token when signin.
For Administrator Only.
Create User on x-www-form-urlencoded with key and value:
username : 
password : 
role : 

7th Step

Using postman go to url : (DELETE) http://localhost:3000/api/users/id fill on Headers key and value
token : your token when signin.
For Administrator Only.
Deleted user with /:id.

File and Folder

.
├── README.md
├── rest-api
│   ├── bin
│   │   └── www
│   └── config
│   │   └── config.json
│   └── controllers
│   │   └── auth.js
│   │   └── users.js
│   └── helpers
│   │   └── auth.js
│   │   └── generateSalt.js
│   │   └── hash.js
│   └── migrations
│   │   └── 20170724040824-create-user.js
│   │   └── 20170724111610-addColumn_Secret_on_table_user.js
│   │   └── 20170724111852-addColumn_role_on_table_user.js
│   └── models
│   │   └── index.js
│   │   └── user.js
│   └── routes
│   │   └── auth.js
│   │   └── index.js
│   │   └── users.js
├── package.json
├── app.js
├── .env.template

rest-api's People

Contributors

pdvega avatar tamatamvan avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.