46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
import express from 'express';
|
|
import nunjucks from 'nunjucks';
|
|
import path from 'node:path';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { formatCents } from './money.js';
|
|
import ordersRouter from './routes/orders.js';
|
|
import customersRouter from './routes/customers.js';
|
|
import clocksRouter from './routes/clocks.js';
|
|
import invoiceRouter from './routes/invoice.js';
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
const app = express();
|
|
|
|
const env = nunjucks.configure(path.join(__dirname, 'views'), {
|
|
autoescape: true,
|
|
express: app,
|
|
});
|
|
env.addFilter('money', (cents: number) => formatCents(cents));
|
|
env.addFilter('lineTotal', (unitPrice: number, quantity: number) =>
|
|
formatCents(Math.round((quantity ?? 1) * (unitPrice ?? 0)))
|
|
);
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
app.use(express.static(path.join(process.cwd(), 'public')));
|
|
app.use(
|
|
'/vendor/htmx',
|
|
express.static(path.join(process.cwd(), 'node_modules/htmx.org/dist'))
|
|
);
|
|
|
|
app.use(ordersRouter);
|
|
app.use('/customers', customersRouter);
|
|
app.use('/clocks', clocksRouter);
|
|
app.use(invoiceRouter);
|
|
|
|
app.use((err: unknown, _req: express.Request, res: express.Response, _next: express.NextFunction) => {
|
|
console.error(err);
|
|
res.status(500).send('Something went wrong.');
|
|
});
|
|
|
|
const PORT = process.env.PORT ? Number(process.env.PORT) : 3000;
|
|
const HOST = '127.0.0.1';
|
|
app.listen(PORT, HOST, () => {
|
|
console.log(`Harding's Clocks running at http://${HOST}:${PORT}`);
|
|
});
|