refactor: added paths helper; refactor: added @since to functions

This commit is contained in:
Roberto Tonino 2021-04-03 19:03:46 +02:00
parent 589f7e9800
commit 8c812812df
7 changed files with 34 additions and 8 deletions

View File

@ -1,4 +1,3 @@
import path from 'path'
import http from 'http'
import express, { Application } from 'express'
@ -11,24 +10,31 @@ import usersRouter from './routes/users'
import { normalizePort } from './helpers/port'
import { getErrorCb, getListeningCb } from './helpers/server-callbacks'
import { WEBUI_DIR } from './helpers/paths'
const ROOT_DIR = path.resolve('../')
const PORT = normalizePort(process.env.PORT || '6595')
const debug = initDebug('deemix-gui:server')
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(path.join(ROOT_DIR, 'webui', 'public')))
app.use(express.static(WEBUI_DIR))
/* === Routes === */
app.use('/', indexRouter)
app.use('/users', usersRouter)
/* === Other === */
app.set('port', PORT)
/* === Server port === */
server.listen(PORT)
/* === Server callbacks === */
server.on('error', getErrorCb(PORT))
server.on('listening', getListeningCb(server, debug))

View File

@ -0,0 +1,4 @@
import path from 'path'
export const ROOT_DIR = path.resolve('../')
export const WEBUI_DIR = path.join(ROOT_DIR, 'webui', 'public')

View File

@ -1,8 +1,11 @@
import { Port } from '../types'
/**
* Normalize a port into a number, string, or false.
*
* @since 0.0.0
*/
export function normalizePort (portString:string) {
export function normalizePort (portString:string): Port {
const port = parseInt(portString, 10)
if (isNaN(port)) {

View File

@ -3,6 +3,8 @@ import type { Debugger } from 'debug'
/**
* Event listener for HTTP server "error" event.
*
* @since 0.0.0
*/
export function getErrorCb (port: number | string | boolean) {
return (error: any) => {
@ -32,6 +34,8 @@ export function getErrorCb (port: number | string | boolean) {
/**
* Event listener for HTTP server "listening" event.
*
* @since 0.0.0
*/
export function getListeningCb (server: http.Server, debug: Debugger) {
return () => {

View File

@ -2,7 +2,11 @@ import express from 'express'
const router = express.Router()
/* GET home page. */
/**
* GET home page
*
* @since 0.0.0
*/
router.get('/', (_, res) => {
res.render('index', { title: 'Express' })
})

View File

@ -2,8 +2,12 @@ import express from 'express'
const router = express.Router()
/* GET users listing. */
router.get('/', function (_, res) {
/**
* GET users listing.
*
* @since 0.0.0
*/
router.get('/', (_, res) => {
res.send('respond with a resource')
})

1
server/src/types.ts Normal file
View File

@ -0,0 +1 @@
export type Port = number | string | boolean