-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (47 loc) · 1.65 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
const express = require('express')
const bodyParser = require('body-parser')
const config = require('./config/config')
const mongodbConnect = require('./db/mongodb')
const userRouter = require('./routes/userRoutes')
const blogRouter = require('./routes/blogRoutes')
const helmet = require('helmet')
const rateLimiter = require('./ratelimiting/rate_limiter')
const logger = require('./logger/logger')
const auth0Middleware = require('./auth/auth0')
const { requiresAuth } = require('express-openid-connect')
const app = express()
// middlewares
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(auth0Middleware)
// app.use(jwtCheck)
// accessible routes
app.use('/api/v1/users', userRouter)
app.use('/api/v1/blogs', blogRouter)
// protected routes
app.use('/api/v1/blogs/createBlog', requiresAuth(), blogRouter)
app.use('/api/v1/blogs/updateBlogState/:id', requiresAuth(), blogRouter)
app.use('/api/v1/blogs/updateByID/:id', requiresAuth(), blogRouter)
app.use('/api/v1/blogs/deleteByID/:id', requiresAuth(), blogRouter)
app.use('/api/v1/blogs/ownersBlogs/:state', requiresAuth(), blogRouter)
// mongodb connection
mongodbConnect
// security middleware
app.use(helmet())
// rate limiting middleware to all requests
app.use(rateLimiter)
// error handler middleware
app.use((err, req, res, next) => {
logger.error(err.message)
res.status(500).send({
error: 'An unexpected error occurred'
})
next()
})
// home route
app.get('/', (req, res) => {
res.send(req.oidc.isAuthenticated() ? 'Logged in' : 'Logged out');
})
app.listen(config.PORT, () => {
logger.info(`server is listening on port: ${config.PORT} `);
})