nm3clol-express-app/app/server.js

107 lines
3.7 KiB
JavaScript

const express = require('express');
const axios = require('axios');
const app = express();
const serve = require('./vercel-serve');
const path = require('path');
const glob = require('glob');
const matter = require('gray-matter');
const ejs = require('ejs');
const helpers = require('../views/helpers/functions');
const search = require('../routes/search');
// const advancedSearch = require('../routes/advanced-search');
// Port number for HTTP server
const port = process.env.PORT||3000;
// Set EJS as the view engine
app.set('view engine', 'ejs');
// Specify the views directory
app.set('views', path.join(__dirname, '..', 'views'));
// Middleware to parse JSON request body
app.use(express.json());
// Middleware to rewrite requests
//app.use(rewriter);
// // Serve static files (CSS, JavaScript, images, etc.)
// app.use(serve('../public', {
// dotfiles: 'ignore',
// index: false,
// }));
// app.get('/', (req, res) => {
// res.send('Hello World!');
// })
// Search endpoints
app.use('/search', search.router);
// app.use('/advanced-search', advancedSearch.router);
// Endpoints for all the site's pages.
glob.globSync('pages/**/*.md', {
cwd: path.join(__dirname, '..'),
matchBase: true,
follow: true,
}).forEach((filePath) => {
const expressRoutePathFromFilePath = (filePath) => {
return filePath.substring('pages'.length, filePath.length - path.extname(filePath).length).replaceAll(path.sep, path.posix.sep);
};
const route = expressRoutePathFromFilePath(filePath);
const fullFilePath = path.join(__dirname, '..', filePath);
let paths = route.split(path.posix.sep);
paths[0] = 'public';
app.get(route, async (req, res) => {
const fm = matter.read(fullFilePath);
const fmData = { fm: fm.data, excerpt: fm.excerpt };
const content = helpers.md.render(fm.content, fmData );
const renderData = { content, route, filePath, fullFilePath, req, paths, ...fmData };
res.render("page", { h: helpers, ...renderData });
});
});
//app.get('/OCR-Encoded-PDFs/Russell-County-Web-Site_2024-02-13_19_50_Modified-With-OCR-Encoding**', rewriter.rewrite('/Web_Site_Archives/Russell_County_Web_Site-2024-02-13_19_50_Modified_With_OCR_Encoding/$1'));
app.get('*', async (req, res) => {
await serve(req, res, {
public: path.join(__dirname, '..', 'public'),
symlinks: true,
trailingSlash: true,
cleanUrls: false,
renderSingle: false,
unlisted: [
".DS_Store",
".git",
"README*"
],
redirects: [
{
source: "/:year(\d{4})-:mo(\d{2})-:dd(\d{2})_:hh(\d{2})_:mm(\d{2})/",
destination: "/Web_Site_Archives/Russell_County_Web_Site-:year-:mo-:dd_:hh_:mm/"
},
{
source: "/OCR-Encoded-PDFs",
destination: "/Web_Site_Archives"
},
{
source: "/OCR-Encoded-PDFs/Russell-County-Web-Site_2024-02-13_19_50_Modified-With-OCR-Encoding.zip",
destination: "/Web_Site_Archives/Russell_County_Web_Site-2024-02-13_19_50_Modified_With_OCR_Encoding.zip"
},
{
source: "/OCR-Encoded-PDFs/Russell-County-Web-Site_2024-02-13_19_50_Modified-With-OCR-Encoding/:u(.*)",
destination: "/Web_Site_Archives/Russell_County_Web_Site-2024-02-13_19_50_Modified_With_OCR_Encoding:u"
},
{ source: '/YouTube Channel', destination: '/Russell_County_BOS/YouTube_Channel' },
{ source: '/YouTube Channel.zip', destination: '/Russell_County_BOS/YouTube_Channel.zip' },
{ source: '/YouTube Channel/:u?', destination: '/Russell_County_BOS/YouTube_Channel/:u' },
{ source: '/Project Reclaim [WI19KR9Ogwg].mkv', destination: '/YouTube_Archives/@VADMME/Project Reclaim [WI19KR9Ogwg].mkv' },
]
});
});
// Start server
app.listen(port, () => {
console.log(`no-moss-3-carbo-landfill-library.online app listening on port ${port}`);
});