-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
272 lines (202 loc) · 7.25 KB
/
server.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
//express tools and configuring
let express = require("express")
let app = express()
let cors = require("cors")
const path = require("path")
const fs = require("fs")
const multer = require("multer")
let bodyParser = require('body-parser')
app.use(cors())
//express configuration
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
// parse application/json
// app.use(bodyParser.json()) /// no need
// parse application/x-www-form-urlencoded
// app.use(bodyParser.urlencoded({extended: true})) //no need
////////mongodb
//// mongo atlas
let mongodb = require("mongodb").MongoClient ///mongodb atlas
const { ObjectID } = require("bson")
// const { markAsUntransferable } = require("worker_threads")
// let ObjectId = require('mongodb').ObjectID;
require("dotenv").config()
////////// pages to send
// app.use(express.static("./public")) ///imgs
app.use(express.static("./public-imgs"))
app.use("/", express.static("./home"))
app.use("/mode", express.static("./mode"))
////////////////////////////////plan
///feature 1; post
///feature 1; get
///feature 2; post
///feature 2; get
///feature 3; post
///feature 3; get
////post request; same route for both json data; currentCoords and form data; image, title,
///get data
///insert them locally
///make an object to be sent to the db; sent coords, title, resulted path
////get request
///get; conts; get all docs from conts collection
///structure it in specific object form
///send it in a res
////////////routes
//////making locs storing plan; to remove
// let storage2 =multer.diskStorage({
// destination: (req, file, cb)=>{
// console.log(req.body)
// console.log(file)
// let dir = `./public/locs;imgs/${req.body.etitle}`
// fs.exists(dir, exist => {
// if (!exist) {
// return fs.mkdir(dir, error => cb(error, dir))
// }
// return cb(null, dir)
// })
// },
// filename: (req, file, cb)=>{
// cb(null, "mainImg.png")
// }
// })
let locImg
let locStoring = multer.diskStorage({
destination: "./public-imgs",
filename: async (req, file, cb)=>{
console.log(file)
locImg = await new Date().toISOString().replace(/:/g, '-') +file.originalname.replaceAll(" ", "")
cb(null, locImg)
}
})
let multerLoc = multer({storage: locStoring})
//////locs
app.post("/locs", multerLoc.any(), (req, res)=>{
console.log(".......locs")
console.log(req.body)
////db
if(req.body.title&&req.body.etitle&&req.body.coords){
mongodb.connect(process.env.MONGOKEY, async (err, client)=>{
let dbb = client.db()
////check if used etitle
if(!await dbb.collection("locs").findOne({etitle: req.body.etitle})){
await dbb.collection("locs").insertOne({
title: req.body.title,
etitle: req.body.etitle,
locImgPath: locImg,
coords: req.body.coords.split(","),
currentConts: [],
dists: []
})
res.sendStatus(200)
}else{
console.log("already used etitle")
res.json({err: "already used etitle"})
}
})
}else{
res.json({err: "no data sent"})
}
})
app.get("/locs", (req, res)=>{
console.log("........get locs")
mongodb.connect(process.env.MONGOKEY, async (err, client)=>{
let dbb = client.db()
let result = await dbb.collection("locs").find({}).toArray()
console.log(result)
res.json(result)
// res.json({title: result.title, etitle: result.etitle, mainImg: result.path, coords: result.coords, dists: result.dists})
})
})
////conts
let contsImgPathList = []
let contStorage = multer.diskStorage({
destination: "./public-imgs",
filename: (req, file, cb)=>{
let path = new Date().toISOString().replace(/:/g, '-') +file.originalname.replaceAll(" ", "")
contsImgPathList.push(path)
cb(null, path)
}
})
let multerCont = multer({storage: contStorage})
/////conts route; no main db, no get
////get into the loc and insert it in the locs conts;
app.post("/conts",(req, res, next)=>{contsImgPathList= []; next()}, multerCont.array("Cont") , (req, res)=>{
console.log(".....conts")
console.log(req.body)
mongodb.connect(process.env.MONGOKEY, async (err, client)=>{
let dbb = client.db()
contsImgPathList.forEach(async e=>{
await dbb.collection("locs").findOneAndUpdate({etitle: req.body.etitle}, {$push: {currentConts: e}})
})
// let found = await dbb.collection("locs").findOne({etitle:req.body.etitle})
// console.log(found)
})
})
// let distsImgPathList = []
let before
let after
let distStorage = multer.diskStorage({
destination: "./public-imgs",
filename: (req, file, cb)=>{
let path = new Date().toISOString().replace(/:/g, '-') +file.originalname.replaceAll(" ", "")
file.fieldname == "before"?before = path: after= path
// distsImgPathList.push(path)
cb(null, path)
}
})
let multerDist = multer({storage: distStorage})
//////dist
app.post("/dist",(req, res, next)=>{before = ""; after = ""; next()}, multerDist.any(), (req, res)=>{
console.log("...............dist")
console.log(req.body)
if(req.body.info&&req.body.etitle&&after&&before){
acceptedConts = req.body.acceptedConts.split(",")
refusedConts = req.body.refusedConts.split(",")
mongodb.connect(process.env.MONGOKEY, async (err, client)=>{
let dbb = client.db()
////remove accepted
if(acceptedConts[0]){
console.log("............accepted")
acceptedConts.forEach(async e=>{
await dbb.collection("locs").findOneAndUpdate({etitle: req.body.etitle}, {$pull: {currentConts: e}})
})
}
if(refusedConts[0]){
////remove refused
console.log(".....refused")
refusedConts.forEach(async e=>{
await dbb.collection("locs").findOneAndUpdate({etitle: req.body.etitle}, {$pull: {currentConts: e}})
})
}
let addDIst = await dbb.collection("locs").findOneAndUpdate({etitle: req.body.etitle}, {$push: {dists: {
before: before,
after: after,
info: req.body.info,
conts: acceptedConts,
}}})
res.sendStatus(200)
})
}else{
res.json({err: "no data sent"})
}
console.log(before)
console.log(after)
})
//////////routes
app.get("/confirmed", (req, res)=>{
console.log(".......get confirmed lines.....")
mongodb.connect(process.env.MONGOKEY2, async (err, client)=>{
let dbb = client.db()
let results = await dbb.collection("confirmed").find().toArray()
res.send(results)
console.log(results)
})
})
/////////////////////test code
// mongodb.connect(process.env.MONGOKEY, async (err, client)=>{
// let dbb = client.db()
// })
/// //check if valid data;
/// if(req.body.?&&req.body.??&&req.body.???[){...res.sendStatus(200)}else{res.json({err:"no data sent"})}
///////establishing
app.listen(process.env.PORT || 3000, ()=>console.log("listennig 3000"))