Basic RESTful api for shopping where you can add/delete your product and order any product after authorization.
- Built a REST Api in Node.js using MVC approch using Node.js + MongoDB.
- JWT for Authetication and Authorization.
- Node.js 6.11.2
- MongoDb
-
Clone or download the repo. into your local system.
-
Cd into that root folder you just cloned locally.
-
install all dependencies which are written in the packet.json file, type
npm install -
Now typing
npm startwill start a server !
App should now be running on localhost:3000
- For dependencies refer Package.json
- Postman extension can be used for testing !
| Routes | Description |
|---|---|
GET/products/ |
Get list of all products |
POST/products/ |
Post a new product |
GET/products/:productId |
Get details of a particular product. |
PATCH/products/:productId |
Update a particular product |
DELETE/products/:productId |
Delete a particular product |
| Routes | Description |
|---|---|
POST/user/signUp |
Sign up a new user |
POST/user/login |
Login a user |
DELETE/user/:userId |
Delete a user from database |
| Routes | Description |
|---|---|
GET/orders/ |
Get all orders by the logged in user |
POST/orders/ |
Post a new order for the logged in user |
GET/orders/:orderId |
Fetch details of a particular order |
DELETE/orders/:orderId |
Deletes a particular order |
Send Get request to fetch the list of all products with seller email in JSON format..
Method: GET
URL: /products/
Produces: application/json
-
Request : /products/
-
Response:
{ "count": 3, "products": [ { "name": "laptop", "price": 12000, "_id": "5c5d8523c5551e1257d0eaab", "productImage": "uploads/2019-02-08T13:33:23.496Zdownload.jpg", "sellerEmail": "[email protected]", "request": { "type": "GET", "url": "https://localhost:3000/products/5c5d8523c5551e1257d0eaab" } }, { "name": "book", "price": 120, "_id": "5c5d8546c5551e1257d0eaac", "productImage": "uploads/2019-02-08T13:33:58.815ZSketch (1).png", "sellerEmail": "[email protected]", "request": { "type": "GET", "url": "https://localhost:3000/products/5c5d8546c5551e1257d0eaac" } }, { "name": "Milk", "price": 50, "_id": "5c5d8594c5551e1257d0eaae", "productImage": "uploads/2019-02-08T13:35:15.814ZScreenshot (69).png", "sellerEmail": "[email protected]", "request": { "type": "GET", "url": "https://localhost:3000/products/5c5d8594c5551e1257d0eaae" } } ]
} ````
User must be logged in to do that.
Method: POST
URL: /products/
Authorization: {token}
Produces: application/json
| Field | Type | Required | Description |
|---|---|---|---|
| name | String | Required | Name of the product |
| price | String | Required | Price of the product |
| productImage | String | Required | Image of the product(.png/.jpeg) |
-
Request:
/products/ -
Response:
{
"message": "Product saved",
"currentProduct": {
"name": "Tea",
"price": 50,
"_id": "5c5d86c3c5551e1257d0eaaf"
},
"request": {
"type": "GET",
"url": "https://localhost:3000/products/5c5d86c3c5551e1257d0eaaf"
}
}
Method: GET
URL: /products/:productId
Produces: application/json
-
Request:
/products/5c5d8523c5551e1257d0eaab -
Response:
{
"product": {
"productSeller": {
"email": "[email protected]"
},
"_id": "5c5d8523c5551e1257d0eaab",
"name": "laptop",
"price": 12000,
"productImage": "uploads/2019-02-08T13:33:23.496Zdownload.jpg"
},
"request": {
"type": "GET",
"url": "https://localhost:3000/products"
}
}
User must be logged in to do that.
| Field | Type | Required | Description |
|---|---|---|---|
| name | String | Not | Updated name of the product |
| price | String | Not | Updated price of the product |
[
{
"propName":"name",
"value":"T.V"
},
{
"propName":"price",
"value":"20000"
}
]
Method: PATCH
URL: /products/:productId
Authorization: {token}
Produces: application/json
-
Request:
/products/5c21e3f5c6cfb46d8c51b616 -
Response:
{
"message": "Product Updated Successfully",
"request": {
"type": "GET",
"description": "Get product details.",
"url": "http://localhost:3000/products/5bdd9d946d97be54a4dc5666"
}
}
User must be logged in to do that.
Method: DELETE
URL: /products/:productId
Authorization: {token}
Produces: application/json
-
Request:
/products/5c21e3f5c6cfb46d8c51b616 -
Response:
{
"message": "Product Deleted",
"request": {
"type": "POST",
"description": "Create new Product",
"url": "http://localhost:3000/products",
"body": {
"name": "String",
"price": "Number",
"productImage": "String"
}
}
}
Sends a POST request to create a new user and returns a web token for further authentication.
Method: POST
URL: /user/signup
Produces: application/json
| Field | Type | Required | Description |
|---|---|---|---|
| Required | User Email | ||
| password | String | Required | password |
-
Request : /user/signUp
-
Response:
{ "message": "User Created!!" }
Sends a POST request to login a exisiting user
Method: POST
URL: /user/login
Produces: application/json
| Field | Type | Required | Description |
|---|---|---|---|
| Required | User Email | ||
| password | String | Required | password |
-
Request:
/user/login -
Response:
{
"message": "Auth Successful!!",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6ImthbHBpdEB0ZXN0LmNvbSIsIklkIjoiNWMyMWUxNGRjNmNmYjQ2ZDhjNTFiNjE1IiwiaWF0IjoxNTQ1NzI0MjcyLCJleHAiOjE1NDU3Mjc4NzJ9.0Ro0iBOO0I_mEjYHhQHPhXy0JmP_iAYhgZAHI3a4vkI"
}
Method: DELETE
URL: /user/:userId
Produces: application/json
-
Request:
/user/:userId -
Response:
{
"message":'User successfully deleted!!'
}
Send Get request to fetch the list of Orders by a User in JSON format. User must be logged in to do that
Method: GET
URL: /orders/
Authorization: {token}
Produces: application/json
-
Request : /orders/
-
Response:
{ "response": { "count": 1, "orders": [ { "_id": "5c5d89d9c5551e1257d0eab0", "productId": { "_id": "5c5d8523c5551e1257d0eaab", "name": "laptop" }, "quantity": 2, "buyer": "5c5d8501c5551e1257d0eaaa", "request": { "type": "GET", "url": "https://api-project-rsinghal26.c9users.io/orders/5c5d89d9c5551e1257d0eab0" } } ] }
User must be logged in to do that.
Method: POST
URL: /orders/
Authorization: {token}
Produces: application/json
| Field | Type | Required | Description |
|---|---|---|---|
| productId | String | Required | Id of the product to be ordered |
| quantity | integer | Required | Quantity |
-
Request:
/orders/ -
Response:
{
"message": "Order Stored",
"currentOrder": {
"_id": "5c5d89d9c5551e1257d0eab0",
"productId": "5c5d8523c5551e1257d0eaab",
"quantity": 2
},
"request": {
"type": "GET",
"url": "https://localhost:3000/orders/5c5d89d9c5551e1257d0eab0"
}
}
User must be logged in to do that
Method: GET
URL: /orders/:orderId
Authorization: {token}
Produces: application/json
-
Request:
/orders/5c21f7d6c6cfb46d8c51b61a -
Response:
{
"order": [
{
"quantity": 2,
"_id": "5c5d89d9c5551e1257d0eab0",
"productId": {
"productSeller": {
"id": "5c5d8501c5551e1257d0eaaa",
"email": "[email protected]"
},
"_id": "5c5d8523c5551e1257d0eaab",
"name": "laptop",
"price": 12000,
"productImage": "uploads/2019-02-08T13:33:23.496Zdownload.jpg",
"__v": 0
}
}
],
"request": {
"type": "GET",
"url": "https://localhost:3000/orders"
}
}
User must be logged in to do that.
Method: DELETE
URL: /orders/:orderId
Authorization: Bearer {token}
Produces: application/json
-
Request:
/orders/5c21f89dc6cfb46d8c51b61c -
Response:
{
"message": "Order Deleted",
"request": {
"type": "POST",
"description": "Place new Order",
"url": "http://localhost:3000/orders/"
}
}