refactor: moved middlewares in dedicated file

This commit is contained in:
Roberto Tonino 2021-04-03 19:07:12 +02:00
parent 8c812812df
commit 85cf0e6d13
2 changed files with 16 additions and 9 deletions

View File

@ -1,8 +1,6 @@
import http from 'http'
import express, { Application } from 'express'
import cookieParser from 'cookie-parser'
import logger from 'morgan'
import initDebug from 'debug'
import indexRouter from './routes'
@ -10,7 +8,7 @@ import usersRouter from './routes/users'
import { normalizePort } from './helpers/port'
import { getErrorCb, getListeningCb } from './helpers/server-callbacks'
import { WEBUI_DIR } from './helpers/paths'
import { registerMiddlewares } from './middlewares'
const PORT = normalizePort(process.env.PORT || '6595')
@ -19,17 +17,13 @@ const app: Application = express()
const server = http.createServer(app)
/* === Middlewares === */
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(WEBUI_DIR))
registerMiddlewares(app)
/* === Routes === */
app.use('/', indexRouter)
app.use('/users', usersRouter)
/* === Other === */
/* === Config === */
app.set('port', PORT)
/* === Server port === */

13
server/src/middlewares.ts Normal file
View File

@ -0,0 +1,13 @@
import type { Application } from 'express'
import logger from 'morgan'
import express from 'express'
import cookieParser from 'cookie-parser'
import { WEBUI_DIR } from './helpers/paths'
export function registerMiddlewares (app: Application) {
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use(express.static(WEBUI_DIR))
}