personal-website/server.js

52 lines
1.7 KiB
JavaScript
Raw Normal View History

const PORT = 8090;
2018-07-23 16:58:23 -04:00
var express = require('express');
const formidable = require('express-formidable');
2018-07-23 16:58:23 -04:00
var app = express();
app.use(formidable());
2021-06-07 20:35:05 -04:00
console.log('Starting express server on port ' + PORT);
2018-07-23 16:58:23 -04:00
// set the view engine to ejs
app.set('view engine', 'ejs');
// add folder for static content:
app.use(express.static(__dirname + '/assets'));
2018-07-23 16:58:23 -04:00
app.get(/\/.*/, function (req, res) {
let pathname = 'pages' + req.path;
let page = pathname.substr(pathname.lastIndexOf('/') + 1);
2021-06-07 20:35:05 -04:00
if (pathname !== null && pathname !== undefined) {
if ((pathname)[pathname.length - 1] === '/') {
pathname += 'index';
page = 'index';
}
if (pathname.includes('projects') && page !== 'index') {
// projects has a custom template that is used for all projects
// so we need to change the pathname that the renderer is using
// that template:
pathname = pathname.substr(0, pathname.lastIndexOf(page));
pathname += 'project_template'
// provide the pagename for project_template to use for main content
page = 'partials/md/projects/' + page;
}
else if (pathname.includes('recipes') && page !== 'index') {
pathname = pathname.substr(0, pathname.lastIndexOf(page));
pathname += 'recipe_template'
// provide the pagename for project_template to use for main content
page = 'partials/md/recipes/' + page;
}
}
console.log('request for path: ' + pathname + ', and page: ' + page);
res.render(pathname, { "page": page });
});
app.post("/new_recipe", function (req, res) {
console.log(JSON.stringify(req.fields));
res.send('mission success');
});
app.listen(PORT);