-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (29 loc) · 1.11 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
const express = require('express')
const addRoute = require('./addRoute.js')
const userId = require('./param/userId.js')
const delayBy = require('./middlewares/delayBy.js')
const isAuthenticated = require('./middlewares/isAuthenticated.js')
const isAdmin = require('./middlewares/isAdmin.js')
const isEditor = require('./middlewares/isEditor.js')
const isUserIdEven = require('./middlewares/isUserIdEven.js')
const requestTime = require('./middlewares/requestTime.js')
const app = express()
const router = express.Router()
router.use(requestTime)
router.param('userId', userId)
addRoute({
router,
method: 'get',
path: '/user/:userId',
authMiddlewares: isAuthenticated,
adminMiddlewares: isAdmin,
nonAdminMiddlewares: [isUserIdEven, delayBy(1000), isEditor],
middlewares: (req, res) => {
const { time, user } = req
const duration = Date.now() - time
res.send(`Moin, <em>${user.name}</em>! The middlewares took <strong>${duration}ms</strong>.`)
}
})
app.use(router)
const [,, port] = process.argv
const server = app.listen(port, () => console.log(`Listening on port ${server.address().port}.`))