commit ea2dd23c6ccd098e0c53bf3935080cf107c2a9cf
parent cf04ebda096c62229e07701d4c949770254d39e9
Author: Ryan Jeffrey <ryan@ryanmj.xyz>
Date: Thu, 20 Jan 2022 19:25:50 -0800
Static handling of files.
Diffstat:
1 file changed, 16 insertions(+), 11 deletions(-)
diff --git a/server.ts b/server.ts
@@ -79,14 +79,11 @@ function lsList(theDir: string, ext: string, ...files: string[]) : LSStat[] {
function lsDir(thePath: string) : LSStat[] {
let fileStats: LSStat[] = [];
- fs.readdir(thePath, (err, files) => {
- if(err) {
- return console.error('Cannot scane directory ', thePath, ": ", err);
- }
+ // TODO error checking.
+ let files = fs.readdirSync(thePath);
- files.forEach((file) => {
- fileStats.push(new LSStat(file));
- });
+ files.forEach((file) => {
+ fileStats.push(new LSStat(file));
});
return fileStats;
@@ -102,7 +99,6 @@ app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());
// TODO maybe a system that exports org to handlebars.
-
// Get the requested post
app.get('/posts/:post', (req, res, next) => {
let post = req.params.post.toLowerCase();
@@ -119,12 +115,20 @@ app.get('/posts', (req, res, next) => {
res.status(200).render('indexWriting');
});
// index.html should be before 404 and after everything else
+// Generate files object.
+const files = lsDir('public/files');
+// Server entry for files.
+app.get('/files', (req, res, next) => {
+ res.status(200).render('files', {
+ entries: files
+ });
+});
-
+// LS everything.
+const frontPageItems = lsList('public', '.html', 'main', 'software', 'sneed', 'files');
app.get('/', (req, res, next) => {
- let test = lsList('public', '.html', 'main', 'software', 'sneed');
res.status(200).render('index', {
- entries: test
+ entries: frontPageItems
});
});
@@ -133,6 +137,7 @@ app.get('*', (req, res) => {
res.status(404).render('404');
});
+// Server initialize.
app.listen(port, () => {
console.log('== Server is listening on port', port);
});