-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (49 loc) · 1.92 KB
/
Copy pathserver.js
File metadata and controls
59 lines (49 loc) · 1.92 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
51
52
53
54
55
56
57
58
59
const express = require('express')
const bodyParser = require('body-parser')
const { promisify } = require('util')
const { checkJwt, readProducts, editProducts } = require('./auth.middleware');
const { sampleProducts } = require('./sampleDate')
const app = express()
app.use(bodyParser.json())
// This is an example of an unprotected route
app.get('/unprotected', (req, res) => {
console.log("REACHED UNPROTECTED ENDPOINT, NO AUTH REQUIRED")
return res.status(200).send("done")
})
// This is another example of a protected route
// NOTE: Middleware also adds the user properties in the request.
app.get('/protected', checkJwt, (req, res) => {
console.log("AUTHENTICATED ROUTE REACHED. USER DETAILS RETRIEVED: ", req.user)
return res.status(200).send(req.user)
})
// The default behavior is to throw an error when the token is invalid,
// so you can add your custom logic to manage unauthorized access as follows:
// NOTE: this needs to be added after assigning the routes
app.use(function (err, req, res, next) {
if (err.name === 'UnauthorizedError') {
res.status(401).send('invalid token...');
}
});
app.get('/sample', checkJwt, readProducts, (req, res) => {
console.log('READ SAMPLE END POINT', req.query)
const limit = Number(req.query.limit) || 5
const offset = Number(req.query.offset) || 0
const data = sampleProducts.slice(offset, offset+limit)
return res.status(200).send(data)
})
app.post('/sample', checkJwt, editProducts, (req, res) => {
console.log('EDIT SAMPLE END POINT')
newProduct = req.body.product
if (newProduct) {
sampleProducts.push(newProduct)
return res.status(200).send(sampleProducts)
} else {
return res.status(400).send('cant read product')
}
})
const startServer = async () => {
const port = process.env.SERVER_PORT || 3000
await promisify(app.listen).bind(app)(port)
console.log(`Listening on port ${port}`)
}
startServer()