-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
89 lines (77 loc) · 3 KB
/
app.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
const express = require("express")
const db = require("./models")
const { User } = require('./models')
const multer = require("multer")
const bodyParser = require("body-parser")
const path = require("path")
const cors = require("cors");
const morgan = require("morgan")
require("dotenv").config()
const app = express()
app.use(bodyParser.json())
const publicdrc = path.resolve(__dirname, 'public')
app.use(express.static(publicdrc))
app.use(bodyParser.urlencoded({
extended: false
}));
const corsOptions = {
origin: '*',
credentials: true,
optionSuccessStatus: 200,
}
app.use(cors(corsOptions))
app.use(morgan('dev'));
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './public/screenshots')
},
filename: (req, file, cb) => {
cb(null, new Date().toISOString().replace(/:/g, '-') + '-' + file.originalname)
}
})
const upload = multer({ storage });
app.put("/user",
upload.single('file'),
async (req, res) => {
const data= await JSON.parse(req.body.data)
console.log(data)
const user = await User.findOne({ email: data.email })
if (user) {
return res.send(JSON.stringify({ status: 403, message: "Email already registered" }))
}
else {
// console.log(req.file.path)
return User.create({
name: data.name,
email: data.email,
phonenumber: data.phonenumber,
collegename: data.collegename,
transactionid: data.transactionid,
imageurl: new Date().toISOString().replace(/:/g, '-') + '-' + req.file.originalname,
isverified: false
}).then((response) => res.send(JSON.stringify({ status: 200, message: "User registered successfully" }))).catch((err) => res.send(JSON.stringify({ status: 400, message: "Couldn't register User" })))
}
})
app.get("/users", async (req, res) => {
if (req.headers.authorization === process.env.TOKEN) {
return User.findAll().then((users) => res.send(JSON.stringify({ status: 200, data: users, message: 'Successfully fetched data' }))).catch((err) => res.send(JSON.stringify({ status: 400, message: "Couldn't fetch Users" })))
}
else {
return res.send(JSON.stringify({ status: 401, message: 'Unauthorized' }))
}
})
app.post("/verify", async (req, res) => {
console.log(process.env.TOKEN)
if (req.headers.authorization === process.env.TOKEN) {
return User.findOne({ email: req.body.email }).then((user) => { user.isverified = true; user.save(); res.send(JSON.stringify({ status: 200, message: 'Successfully verified user' })) }).catch((err) => res.send(JSON.stringify({ status: 400, message: "Couldn't verify Users" })))
}
else {
return res.send(JSON.stringify({ status: 401, message: 'Unauthorized' }))
}
})
app.get("/", (req,res)=>res.send({"name": "Vinay"}))
db.sequelize.sync().then(req => {
app.listen(3009, () => {
console.log("server running");
})
});