-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
50 lines (41 loc) · 1.22 KB
/
Copy pathapp.js
File metadata and controls
50 lines (41 loc) · 1.22 KB
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
////////////////////////////////////////////////////
/////////////// Basic Initialization ///////////////
////////////////////////////////////////////////////
const express = require('express')
const bodyParser = require('body-parser')
const app = express()
app.use(bodyParser.json())
//Helper Functions
const { ErrorHandler, handleError } = require('./helpers/error')
//Const
const PORT = 5000 || process.env.PORT
//GET
//Test Function
app.get('/', (req, res) => {
res.end(`${Date.now()}`)
})
//POST
//Login Function
app.post('/login', async (req, res, next) => {
res.setHeader("Content-Type", "application/json")
try {
const { email, password } = req.body
if (!email || !password) {
throw new ErrorHandler(404, "Missing required email and password fields")
}
let user = db.user.findOne({ email: email, password: password})
if (!user) {
throw new ErrorHandler(404, 'User with the specified email does not exists')
}
//Handle your normal scenario
return res.status(200).end("success")
} catch (error) {
next(error)
}
});
app.use((err, req, res, next) => {
if (err) {
handleError(err, res);
}
});
app.listen(PORT);