Skip to content

Commit daf815b

Browse files
author
Bhadresh Pithwa
committed
Web services API basic structure added.
- Added API for Register, Login , - Add/Update/Delete User, - Get single user, multiple user data. - JWT Authentication. - Basic sample for Product, Order, Cart - Basic sample Routes for Product, Order, Cart
1 parent 3b96e73 commit daf815b

File tree

15 files changed

+3930
-0
lines changed

15 files changed

+3930
-0
lines changed

web_services/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
DB_URL_DEVELOPMENT=mongodb://localhost/mern_ecommerce
2+
API_PORT=5002
3+
PASS_SECRET=SHRkKEGzfelIeTAWLu4j
4+
JWT_SECRET=uYzamxJH81SZFmeZap3oz1N1jNRhD2B3

web_services/.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# production
12+
/build
13+
14+
# misc
15+
.DS_Store
16+
.env.local
17+
.env.development.local
18+
.env.test.local
19+
.env.production.local
20+
21+
npm-debug.log*
22+
yarn-debug.log*
23+
yarn-error.log*

web_services/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,31 @@
1+
const express = require('express');
2+
const app = express();
3+
const mongoose = require("mongoose");
4+
const dotenv = require("dotenv");
5+
const authRoute = require("./routes/auth");
6+
const userRoute = require("./routes/user");
17

8+
// Read value from .env file
9+
dotenv.config();
10+
11+
//MongoDB Connection
12+
mongoose.connect(process.env.DB_URL_DEVELOPMENT)
13+
.then(()=>{
14+
console.log("DB Connection Successfully")
15+
}).catch((err)=>{
16+
console.log(err)
17+
})
18+
19+
// parse requests of content-type - application/json, Read JSON data from request
20+
app.use(express.json());
21+
22+
//Use routes
23+
app.use("/api/auth",authRoute);
24+
app.use("/api/users",userRoute);
25+
26+
//Read PORT from .env file OR Default set 5002
27+
const API_PORT = process.env.API_PORT || 5002;
28+
29+
app.listen(API_PORT,()=>{
30+
console.log(`Backend Server is running on port ${API_PORT}`)
31+
})

web_services/models/Cart.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const mongoose = require("mongoose");
2+
3+
const CartSchema = new mongoose.Schema(
4+
{
5+
userId:{type:String, required:true},
6+
products:[
7+
{
8+
productId:{
9+
type: String,
10+
},
11+
quantity:{
12+
type: Number,
13+
default:1
14+
}
15+
16+
}
17+
]
18+
},
19+
{timestamps:true}
20+
);
21+
22+
module.exports = mongoose.model("Cart",CartSchema);

web_services/models/Order.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const mongoose = require("mongoose");
2+
3+
const OrderSchema = new mongoose.Schema(
4+
{
5+
userId:{type:String, required:true},
6+
products:[
7+
{
8+
productId:{
9+
type: String,
10+
},
11+
quantity:{
12+
type: Number,
13+
default:1
14+
}
15+
16+
}
17+
]
18+
},
19+
amount: {type:Number, required:true},
20+
address: {type:Object, required:true},
21+
status: {type:String, default:"pending"},
22+
{timestamps:true}
23+
);
24+
25+
module.exports = mongoose.model("Cart",OrderSchema);

web_services/models/Product.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const mongoose = require("mongoose");
2+
3+
const ProductSchema = new mongoose.Schema(
4+
{
5+
title:{type:String, required:true},
6+
description:{type:String, required:true},
7+
image:{type:String, required:true},
8+
categories:{type:Array},
9+
size:{type:String},
10+
color:{type:String},
11+
price:{type:String},
12+
},
13+
{timestamps:true}
14+
);
15+
16+
module.exports = mongoose.model("Product",ProductSchema);

web_services/models/User.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const mongoose = require("mongoose");
2+
3+
4+
const UserSchema = new mongoose.Schema(
5+
{
6+
username:{type:String, required:true,unique:true},
7+
email:{type:String, required:true,unique:true},
8+
password:{type:String, required:true},
9+
isAdmin:{type:Boolean, default:false}
10+
},
11+
{timestamps:true}
12+
);
13+
14+
module.exports = mongoose.model("User",UserSchema);

0 commit comments

Comments
 (0)