Get a full fake REST API as soon as possible. Base on NodeJS + JSON Server.
Support more features:
-
Register user with username & password or email & password.
-
Login with registered users.
-
Protect resources using JWT Bearer authentication.
-
Upload files.
Preview: https://fake-rest-api-nodejs.herokuapp.com
git clone https://github.com/robinhuy/fake-rest-api-nodejs.gitor fork to your account and clone the forked repo
cd fake-rest-api-nodejs
npm installor if you using yarn
cd fake-rest-api-nodejs
yarn install-
Production mode:
npm start
or
yarn start
-
Development mode (auto reload server when editing using nodemon):
npm run dev
or
yarn dev
-
The server will run on
http://localhost:3000. You can test with public endpoint:http://localhost:3000/products(GET method).
All the data was placed in database.json. Edit it to suit your purpose.
You can use https://mockaroo.com/ to mock data, and publish your code to https://heroku.com/ or similar hosting to get a Public API.
Note:
-
To protect resources, decleare resources and protected methods in
database.json:"protected_resources": { "users": ["GET", "POST", "PUT", "PATCH", "DELETE"], "products": ["POST", "PUT", "PATCH", "DELETE"] }
-
To register new user, using endpoint
/register, methodPOST, request typeapplication/json. Body request likeusersresources: -
To login, using endpoint
/login, methodPOST, request typeapplication/json. Body request like this:{ "username": "admin", "password": "admin" }or
{ "email": "admin@gmail.com", "password": "admin" } -
To upload single file, using endpoint
/upload-file, methodPOST, request typeform-data, fieldfile. Uploaded file stored in/public/uploads/. -
To upload multiple files, using endpoint
/upload-files, methodPOST, request typeform-data, fieldfiles. Uploaded files stored in/public/uploads/. -
Change default port, database file, jwt secret or jwt token expires in
config.json.
Please view detailed document in https://github.com/typicode/json-server/blob/master/README.md#table-of-contents
If you want to change logic of authentication or add more feature, please edit file server.js or additional_routes.js.
View and modify resources in database.json.
Open endpoints require no Authentication.
-
Login: POST /login
-
Register: POST /register
-
Get products: GET /products
-
Get product by ID: GET /products/:id
-
Upload single file: POST /upload-file
-
Upload multiple files: POST /upload-files
Private endpoints require a valid Token to be included in the header of the request. A Token can be acquired from the Login view above.
-
Get users: GET /users
-
Get user by ID: GET /users/:id
-
Create user: POST /users
-
Update user (entire information): PUT /users/:id
-
Update user (partial information) PATCH /users/:id
-
Create product: POST /products
-
Update product (entire information): PUT /products/:id
-
Update product (partial information) PATCH /products/:id