Import of glitchdotcom/glitch-hello-node

This commit is contained in:
Glitch (glitch-hello-node) committed 2021-04-09 20:57:05 +00:00
1 parent 2c1a9fb34c
commit 6153c101b0
14 files changed
+967 -1242

No files matched your search

+52 -6
View File
@@ -1,5 +1,4 @@
const path = require("path");
const flavors = require("./src/api/flavors");
// Require the framework and instantiate it
const fastify = require("fastify")({
@@ -12,13 +11,60 @@ fastify.register(require("fastify-static"), {
prefix: "/" // optional: default '/'
});
// Our home page route, this pulls from public/index.html
// but you don't have to send a html, or even have a webpage!
fastify.get("/", function(request, reply) {
return reply.sendFile("index.html");
// fastify-formbody lets us parse incoming forms
fastify.register(require("fastify-formbody"));
// point-of-view is a templating manager for fastify
fastify.register(require("point-of-view"), {
engine: {
handlebars: require("handlebars")
}
});
fastify.get("/api/flavors", flavors);
// load and parse SEO data
const seo = require("./src/seo.json");
if (seo.url === "glitch-default") {
seo.url = `https://${process.env.PROJECT_DOMAIN}.glitch.me`;
}
// Our home page route, this pulls from src/pages/index.hbs
fastify.get("/", function(request, reply) {
let params = { seo: seo };
if (request.query.randomize) {
const colors = require("./src/colors.json");
const allColors = Object.keys(colors);
let currentColor = allColors[(allColors.length * Math.random()) << 0];
params = {
color: colors[currentColor],
colorError: null,
seo: seo
};
}
reply.view("/src/pages/index.hbs", params);
});
// A route to handle form submissions and output the main page with custom colors
fastify.post("/", function(request, reply) {
let params = { seo: seo };
let color = request.body.color;
if (color) {
const colors = require("./src/colors.json");
color = color.toLowerCase().replace(/\s/g, "");
if (colors[color]) {
params = {
color: colors[color],
colorError: null,
seo: seo
};
} else {
params = {
colorError: request.body.color,
seo: seo
};
}
}
reply.view("/src/pages/index.hbs", params);
});
// Run the server and report out to the logs
fastify.listen(process.env.PORT, function(err, address) {