2025-08-06 23:29:48 -05:00
|
|
|
import cookieParser from 'cookie-parser'
|
|
|
|
|
import cors from 'cors'
|
|
|
|
|
import express from 'express'
|
|
|
|
|
import createError from 'http-errors'
|
|
|
|
|
import logger from 'morgan'
|
|
|
|
|
import path from 'path'
|
|
|
|
|
import { fileURLToPath } from 'url'
|
|
|
|
|
import xss from 'xss-clean'
|
2025-07-24 18:08:31 -05:00
|
|
|
|
2025-08-06 23:29:48 -05:00
|
|
|
import indexRouter from './routes/index.js'
|
|
|
|
|
import postsRouter from './routes/posts.js'
|
|
|
|
|
import usersRouter from './routes/users.js'
|
2025-07-24 18:08:31 -05:00
|
|
|
|
2025-08-06 23:29:48 -05:00
|
|
|
const __filename = fileURLToPath(import.meta.url)
|
|
|
|
|
const __dirname = path.dirname(__filename)
|
2025-07-24 18:08:31 -05:00
|
|
|
|
2025-08-06 23:29:48 -05:00
|
|
|
const app = express()
|
2025-07-24 18:08:31 -05:00
|
|
|
|
|
|
|
|
// view engine setup
|
2025-08-06 23:29:48 -05:00
|
|
|
app.set('views', path.join(__dirname, 'views'))
|
|
|
|
|
app.set('view engine', 'jade')
|
2025-07-24 18:08:31 -05:00
|
|
|
|
2025-08-06 23:29:48 -05:00
|
|
|
app.use(cors())
|
|
|
|
|
app.options('*', cors())
|
2025-07-25 14:44:19 -05:00
|
|
|
|
2025-08-06 23:29:48 -05:00
|
|
|
app.use(logger('dev'))
|
|
|
|
|
app.use(express.json())
|
|
|
|
|
app.use(express.urlencoded({ extended: false }))
|
|
|
|
|
app.use(cookieParser())
|
|
|
|
|
app.use(express.static(path.join(__dirname, 'public')))
|
2025-07-25 14:44:19 -05:00
|
|
|
// Data sanitization against XSS
|
2025-08-06 23:29:48 -05:00
|
|
|
app.use(xss())
|
2025-07-24 18:08:31 -05:00
|
|
|
|
2025-08-06 23:29:48 -05:00
|
|
|
app.use('/', indexRouter)
|
|
|
|
|
app.use('/api/v1/users', usersRouter)
|
|
|
|
|
app.use('/api/v1/posts', postsRouter)
|
2025-07-24 18:08:31 -05:00
|
|
|
|
|
|
|
|
// catch 404 and forward to error handler
|
2025-08-06 23:29:48 -05:00
|
|
|
app.use(function (req, res, next) {
|
|
|
|
|
next(createError(404))
|
|
|
|
|
})
|
2025-07-24 18:08:31 -05:00
|
|
|
|
|
|
|
|
// error handler
|
2025-08-06 23:29:48 -05:00
|
|
|
app.use(function (err, req, res, next) {
|
|
|
|
|
// set locals, only providing error in development
|
|
|
|
|
res.locals.message = err.message
|
|
|
|
|
res.locals.error = req.app.get('env') === 'development' ? err : {}
|
2025-07-24 18:08:31 -05:00
|
|
|
|
2025-08-06 23:29:48 -05:00
|
|
|
// render the error page
|
|
|
|
|
res.status(err.status || 500)
|
|
|
|
|
res.render('error')
|
|
|
|
|
})
|
2025-07-24 18:08:31 -05:00
|
|
|
|
2025-08-06 23:29:48 -05:00
|
|
|
export default app
|