- Built a REST API in Node.js using the MVC approach by leveraging Node.js + MongoDB.
- User JWT for Authentication and Authorization.
- A RESTful API built for serving as a Backend for a Shopping Cart.
For development, you will only need Node.js and a node global package, Yarn, installed in your environement.
-
Just go on official Node.js website and download the installer. Also, be sure to have
git
available in your PATH,npm
might need it (You can find git here). -
You can install nodejs and npm easily with apt install, just run the following commands.
$ sudo apt install nodejs $ sudo apt install npm
-
You can find more information about the installation on the official Node.js website and the official NPM website.
If the installation was successful, you should be able to run the following command.
$ node --version
v8.11.3
$ npm --version
6.1.0
If you need to update npm
, you can make it using npm
! Cool right? After running the following command, just open again the command line and be happy.
$ npm install npm -g
$ git clone https://github.com/vinod2305/Shopping-RESTapi.git
$ cd Shopping-RESTapi
$ npm install
$ npm start
Endpoint:
Method: POST
Type: RAW
URL: http://localhost:3000/user/signup
Body:
{
"email":"[email protected]",
"password": "password"
}
{
"message": "User created"
}
Status Code: 201
Endpoint:
Method: POST
Type: RAW
URL: http://localhost:3000/user/login
Body:
{
"email":"[email protected]",
"password": "password"
}
{
"message": "Auth successful",
"token": {token}
}
Status Code: 201
Endpoint:
Method: DELETE
Type:
URL: http://localhost:3000/user/60ddabe0d864375a684dc930
{
"message": "User deleted"
}
Status Code: 200
Endpoint:
Method: GET
Type:
URL: http://localhost:3000/products
{
"count": 1,
"products": [
{
"name": "Test product",
"price": 100,
"_id": "60ddb145d864375a684dc933",
"productImage": "uploads/2021-07-01T12:12:53.719Ztestproduct.jpeg",
"request": {
"type": "GET",
"url": "http://localhost:3000/products/60ddb145d864375a684dc933"
}
}
]
}
Status Code: 200
Endpoint:
Method: GET
Type:
URL: http://localhost:3000/products/60ddb145d864375a684dc933
{
"product": {
"_id": "60ddb145d864375a684dc933",
"name": "Test product",
"price": 100,
"productImage": "uploads/2021-07-01T12:12:53.719Ztestproduct.jpeg"
},
"request": {
"type": "GET",
"description": "GET ALL PRODUCTS",
"url": "http://localhost/product"
}
}
Status Code: 200
Endpoint:
Method: POST
Type: FORMDATA
URL: http://localhost:3000/products
Body:
Key | Value | Description |
---|---|---|
name | Test product | Name of the product |
price | 100 | Price of product |
productImage | File | |
More example Requests/Responses: |
{
"message": "Created product successfully",
"createdProduct": {
"name": "Test product",
"price": 100,
"_id": "60de8d65024f812237aec95e",
"productImage": "uploads/2021-07-02T03:52:05.675Ztestproduct.jpeg",
"request": {
"type": "GET",
"url": "http://localhost:3000/products/60de8d65024f812237aec95e"
}
}
}
Status Code: 201
Endpoint:
Method: PATCH
Type: RAW
URL: http://localhost:3000/products/60ddb145d864375a684dc933
Body:
[
{
"price": "150"
}
]
{
"message": "Product updated",
"request": {
"type": "GET",
"url": "http://localhost/product/60ddb145d864375a684dc933"
}
}
Status Code: 200
Endpoint:
Method: DELETE
Type:
URL: http://localhost:3000/products/60ddb145d864375a684dc933
{
"message": "Product Deleted",
"request": {
"type": "POST",
"url": "http://localhost:3000/products",
"body": {
"name": "String",
"price": "Number"
}
}
}
Status Code: 200
Endpoint:
Method: GET
Type:
URL: http://localhost:3000/orders
{
"count": 1,
"orders": [
{
"_id": "60ddb47ad864375a684dc936",
"product": {
"_id": "60ddb395d864375a684dc934",
"name": "Test product"
},
"quantity": 20,
"request": {
"type": "GET",
"url": "http://localhost:3000/orders/60ddb47ad864375a684dc936"
}
}
]
}
Status Code: 200
Endpoint:
Method: GET
Type:
URL: http://localhost:3000/orders/60ddb47ad864375a684dc936
{
"order": {
"quantity": 20,
"_id": "60ddb47ad864375a684dc936",
"product": {
"_id": "60ddb395d864375a684dc934",
"name": "Test product",
"price": 100,
"productImage": "uploads/2021-07-01T12:22:45.184Ztestproduct.jpeg",
"__v": 0
},
"__v": 0
},
"request": {
"type": "GET",
"url": "http://localhost:3000/orders"
}
}
Status Code: 200
Endpoint:
Method: POST
Type: RAW
URL: http://localhost:3000/orders
Body:
{
"productId": "60ddb395d864375a684dc934",
"quantity": "20"
}
{
"message": "Order stored",
"createdOrder": {
"_id": "60ddb42bd864375a684dc935",
"product": "60ddb395d864375a684dc934",
"quantity": 20
},
"request": {
"type": "GET",
"url": "http://localhost:3000/orders/60ddb42bd864375a684dc935"
}
}
Status Code: 201
Endpoint:
Method: DELETE
Type:
URL: http://localhost:3000/orders/60ddb47ad864375a684dc936
{
"message": "Order deleted",
"request": {
"type": "POST",
"url": "http://localhost:3000/orders",
"body": {
"productId": "ID",
"quantity": "Number"
}
}
}
Status Code: 200