2018-07-30 16:59:41 -04:00
|
|
|
const PORT = 8090;
|
2018-07-23 16:58:23 -04:00
|
|
|
var express = require('express');
|
2022-07-03 09:53:37 -04:00
|
|
|
// formidable helps get JSON POST request content
|
2022-07-03 00:11:53 -04:00
|
|
|
const formidable = require('express-formidable');
|
2022-07-03 09:53:37 -04:00
|
|
|
var compile = require('es6-template-strings/compile'),
|
|
|
|
resolveToString = require('es6-template-strings/resolve-to-string'),
|
|
|
|
fs = require('fs');
|
|
|
|
// create a function that can convert from recipe template literal -> new recipe markdown
|
|
|
|
const recipeMarkdownDir = "./recipes/";
|
|
|
|
recipe_template_raw = fs.readFileSync(recipeMarkdownDir + "template/recipe_template.md", "utf-8");
|
|
|
|
compiledRecipeTemplate = compile(recipe_template_raw);
|
|
|
|
|
2018-07-23 16:58:23 -04:00
|
|
|
var app = express();
|
|
|
|
|
2022-07-03 00:11:53 -04:00
|
|
|
app.use(formidable());
|
|
|
|
|
2021-06-07 20:35:05 -04:00
|
|
|
console.log('Starting express server on port ' + PORT);
|
2018-07-25 08:23:17 -04:00
|
|
|
|
2018-07-23 16:58:23 -04:00
|
|
|
// set the view engine to ejs
|
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
|
2018-07-25 08:23:17 -04:00
|
|
|
// add folder for static content:
|
|
|
|
app.use(express.static(__dirname + '/assets'));
|
2018-07-23 16:58:23 -04:00
|
|
|
|
2022-06-20 21:55:50 -04:00
|
|
|
app.get(/\/.*/, function (req, res) {
|
2018-07-27 13:32:34 -04:00
|
|
|
let pathname = 'pages' + req.path;
|
2018-07-31 16:09:51 -04:00
|
|
|
let page = pathname.substr(pathname.lastIndexOf('/') + 1);
|
2021-06-07 20:35:05 -04:00
|
|
|
|
2018-07-31 16:09:51 -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
|
2019-11-22 00:43:29 -05:00
|
|
|
// that template:
|
2018-07-31 16:09:51 -04:00
|
|
|
pathname = pathname.substr(0, pathname.lastIndexOf(page));
|
|
|
|
pathname += 'project_template'
|
2019-11-22 00:43:29 -05:00
|
|
|
// provide the pagename for project_template to use for main content
|
2022-06-20 21:55:50 -04:00
|
|
|
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;
|
2018-07-31 16:09:51 -04:00
|
|
|
}
|
2018-07-27 13:32:34 -04:00
|
|
|
}
|
2018-07-31 16:09:51 -04:00
|
|
|
console.log('request for path: ' + pathname + ', and page: ' + page);
|
2018-07-27 13:32:34 -04:00
|
|
|
|
2022-06-20 21:55:50 -04:00
|
|
|
res.render(pathname, { "page": page });
|
2018-07-27 13:32:34 -04:00
|
|
|
});
|
|
|
|
|
2022-07-03 09:53:37 -04:00
|
|
|
function toCamelCase(str) {
|
|
|
|
return str.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
|
|
|
|
return index == 0 ? word.toLowerCase() : word.toUpperCase();
|
|
|
|
}).replace(/\s+/g, '');
|
|
|
|
}
|
|
|
|
|
|
|
|
// When a user successfully submits a new recipe a post request with
|
|
|
|
// JSON content of recipe data ends up here
|
2022-07-03 00:11:53 -04:00
|
|
|
app.post("/new_recipe", function (req, res) {
|
2022-07-03 09:53:37 -04:00
|
|
|
let dataIn = req.fields;
|
|
|
|
|
|
|
|
filename = recipeMarkdownDir + toCamelCase(dataIn.recipe_name_input) + ".md";
|
|
|
|
if (fs.existsSync(recipeMarkdownDir + filename)) {
|
|
|
|
res.status(409).send(`Already have a recipe with name "${dataIn.recipe_name_input}"`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: more input checking
|
|
|
|
|
|
|
|
// add a '#' to all the tags in the input list
|
|
|
|
formatted_tags = dataIn.tag_input.split(",")
|
|
|
|
.map(elem => "#" + elem)
|
|
|
|
.join(" ");
|
|
|
|
|
|
|
|
dataIn.tags = formatted_tags
|
|
|
|
|
|
|
|
formattedMarkdownRecipe = resolveToString(compiledRecipeTemplate, dataIn);
|
|
|
|
fs.writeFileSync(filename, formattedMarkdownRecipe, 'utf-8');
|
|
|
|
|
|
|
|
res.send('Successfully added!');
|
2022-07-03 00:11:53 -04:00
|
|
|
});
|
|
|
|
|
2019-11-22 00:43:29 -05:00
|
|
|
app.listen(PORT);
|