diff --git a/server/src/app.ts b/server/src/app.ts index 9095eaa..aab9032 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -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)) diff --git a/server/src/helpers/paths.ts b/server/src/helpers/paths.ts new file mode 100644 index 0000000..87d1218 --- /dev/null +++ b/server/src/helpers/paths.ts @@ -0,0 +1,4 @@ +import path from 'path' + +export const ROOT_DIR = path.resolve('../') +export const WEBUI_DIR = path.join(ROOT_DIR, 'webui', 'public') diff --git a/server/src/helpers/port.ts b/server/src/helpers/port.ts index 0b8eb8b..3035c4c 100644 --- a/server/src/helpers/port.ts +++ b/server/src/helpers/port.ts @@ -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)) { diff --git a/server/src/helpers/server-callbacks.ts b/server/src/helpers/server-callbacks.ts index 84e1ee5..18fb1f0 100644 --- a/server/src/helpers/server-callbacks.ts +++ b/server/src/helpers/server-callbacks.ts @@ -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 () => { diff --git a/server/src/routes/index.ts b/server/src/routes/index.ts index 7cf3089..be2ba5e 100644 --- a/server/src/routes/index.ts +++ b/server/src/routes/index.ts @@ -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' }) }) diff --git a/server/src/routes/users.ts b/server/src/routes/users.ts index 02c7a6a..ba5cc06 100644 --- a/server/src/routes/users.ts +++ b/server/src/routes/users.ts @@ -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') }) diff --git a/server/src/types.ts b/server/src/types.ts new file mode 100644 index 0000000..b166801 --- /dev/null +++ b/server/src/types.ts @@ -0,0 +1 @@ +export type Port = number | string | boolean