-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (58 loc) · 1.71 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require("express")
const cors = require("cors")
const path = require("path")
let mongoose = require("mongoose")
let config = require("./config.json")
let Schema = mongoose.Schema
let ObjectId = Schema.ObjectId
let app = express()
const PORT = process.env.PORT || 5000
app.use(express.static(path.join(__dirname+"/public")))
/* To handle the POST data */
app.use(express.urlencoded({ extended: false }));
/* To always fetch data in JSON format */
app.use(express.json())
/* To communicate between servers */
app.use(cors())
/* Creating the model for MongoDB */
let Receipt = mongoose.model("Receipt",Schema({
id : ObjectId,
num : Number,
idd : Number,
quantity:Number,
name:String,
desc:String,
price:Number,
img:String
}))
const string_mongo = `mongodb+srv://${config.username}:${config.password}@cluster0.t0n6iqu.mongodb.net/${config.dbname}?retryWrites=true&w=majority`
mongoose.connect(string_mongo).then((res)=>console.log("Connected"))
.catch((err)=>console.log("Error",err))
app.get("/cart",(req,res)=>{
res.redirect("/")
})
app.get("/admin",(req,res)=>{
res.redirect("/")
})
app.get("/thankyou",(req,res)=>{
res.redirect("/")
})
app.get("/data",(req,res)=>{
Receipt.find().then(dbres=>res.json(dbres))
})
app.post("/users", (req, res) => {
var a = req.body
a.map((val)=>{
let hero = new Receipt({
num : Number(val.num),
idd : Number(val.id),
quantity:String(val.quantity),
name:String(val.name),
desc:String(val.desc),
price:String(val.price),
img:String(val.img)
})
hero.save()
})
});
app.listen(PORT)