-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.mts
84 lines (73 loc) · 2.45 KB
/
app.mts
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* This file:
* 1. Migrated from CJS module to ESM.
* 2. Defined with appropriate TS types.
* 3. Moved from legacy `./app.js` to `./app.mts` to run development server and compile for production.
*
* @module app
*/
import type { Express, ErrorRequestHandler, RequestHandler } from 'express';
import nodeFs from 'node:fs';
import nodePath from 'node:path';
import { fileURLToPath } from 'node:url';
import cookieParser from 'cookie-parser';
import 'dotenv/config';
import express from 'express';
import createError from 'http-errors';
import logger from 'morgan';
import { HTTP } from './library/constants.mts';
import { SIZE, SIZE_MIN } from './library/graycraft.mts';
import routerIndex from './routes/index.mts';
import graycraft from './source/graycraft.umd.js';
const {
STATUS: { INTERNAL_SERVER_ERROR },
} = HTTP,
__filename = fileURLToPath(import.meta.url),
__dirname = nodePath.dirname(__filename),
app: Express = express();
app.set('views', nodePath.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(nodePath.join(__dirname, 'public')));
app.use('/', routerIndex);
/**
* Catch 404 and forward to error handler.
*/
app.use(((req, res, next) => {
const { HOSTNAME, PORT, PORT_PROXY } = process.env,
host = HOSTNAME + ':' + (req.app.get('env') === 'development' ? PORT : PORT_PROXY),
cssBuffer = nodeFs.readFileSync('public/stylesheets/style.css'),
css = String(cssBuffer),
{ back: backQuery, fore: foreQuery, size: sizeQuery } = req.query,
back = String(backQuery ?? 'transparent'),
fore = String(foreQuery ?? ''),
size = Number(sizeQuery ?? SIZE) < SIZE_MIN ? SIZE_MIN : Number(sizeQuery ?? SIZE),
{ getYear, hsl } = graycraft(size, fore, back);
res.render('404', {
back,
css,
host,
header: '404',
hsl,
imagePath: 'images/graycraft.png',
paragraph: 'This page is not found on the server.',
size,
title: 'Not Found',
year: getYear(),
});
next(createError(404));
}) as RequestHandler);
/**
* Error handler.
*/
app.use(((error, req, res) => {
/** Set locals, only providing error in development. */
res.locals.message = error?.message;
res.locals.error = req.app.get('env') === 'development' ? error : {};
res.status(error.status || INTERNAL_SERVER_ERROR);
res.render('error');
}) as ErrorRequestHandler);
export default app;