ampcast/server.js

50 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-07-20 09:29:42 +08:00
const {resolve} = require('path');
const express = require('express');
const Router = express.Router;
const host = 'localhost';
const port = 8000;
const app = express();
const webServer = Router();
2022-08-08 06:05:39 +08:00
const wwwDir = resolve(__dirname, './www');
2021-07-20 09:29:42 +08:00
const devDir = resolve(__dirname, './www-dev');
2022-08-08 06:05:39 +08:00
const webIndex = resolve(wwwDir, './index.html');
2021-07-20 09:29:42 +08:00
2022-12-29 03:21:10 +08:00
const runtimeDir = process.argv[2] === '--prod' ? wwwDir : devDir;
2023-08-10 22:04:04 +08:00
express.static.mime.define({
'application/json': ['json'],
'image/vnd.microsoft.icon': ['ico'],
'image/png': ['png'],
'image/svg+xml': ['svg'],
});
2021-07-20 09:29:42 +08:00
webServer.get('/', (_, res) => res.sendFile(webIndex));
2023-08-10 22:04:04 +08:00
webServer.use('/apple-touch-icon.png', express.static(resolve(wwwDir, './apple-touch-icon.png')));
webServer.use('/favicon.ico', express.static(resolve(wwwDir, './favicon.ico')));
webServer.use('/favicon.svg', express.static(resolve(wwwDir, './favicon.svg')));
webServer.use('/icon-192.png', express.static(resolve(wwwDir, './icon-192.png')));
webServer.use('/icon-512.png', express.static(resolve(wwwDir, './icon-512.png')));
webServer.use('/manifest.json', express.static(resolve(wwwDir, './manifest.json')));
2022-08-08 06:05:39 +08:00
webServer.use('/auth', express.static(resolve(wwwDir, './auth')));
2022-12-29 03:21:10 +08:00
webServer.get('/bundle.css', async (_, res) => res.sendFile(resolve(runtimeDir, `./bundle.css`)));
webServer.get('/:id.js', async (req, res) =>
res.sendFile(resolve(runtimeDir, `./${req.params.id}.js`))
);
webServer.get('/lib/:id.js', async (req, res) =>
res.sendFile(resolve(runtimeDir, `./lib/${req.params.id}.js`))
);
2021-07-20 09:29:42 +08:00
app.use('/', webServer);
app.get('*', (_, res) => res.redirect('/'));
app.listen(port, host, () => {
2023-06-06 02:03:14 +08:00
const timestamp = new Date().toLocaleString();
2021-07-20 09:29:42 +08:00
console.info(`Serving from: http://${host}:${port}`);
2022-12-29 03:21:10 +08:00
console.info(`Using files from: ${runtimeDir}`);
2021-07-20 09:29:42 +08:00
console.info(`Server started at: ${timestamp}`);
});