Import of glitchdotcom/glitch-hello-node

This commit is contained in:
Glitch (glitch-hello-node) committed 2021-05-26 21:54:31 +00:00
1 parent 180d799c09
commit 6fd4e86a6d
4 files changed
+98 -31

No files matched your search

+26 -12
View File
@@ -1,11 +1,19 @@
/*
This is the main Node.js server script for your project
- The two endpoints this back-end provides are defined in fastify.get and fastify.post below
*/
const path = require("path");
// Require the fastify framework and instantiate it
const fastify = require("fastify")({
// set this to true for detailed logging:
// Set this to true for detailed logging:
logger: false
});
// ADD FAVORITES ARRAY VARIABLE FROM README HERE
// Setup our static files
fastify.register(require("fastify-static"), {
root: path.join(__dirname, "public"),
@@ -22,58 +30,64 @@ fastify.register(require("point-of-view"), {
}
});
// load and parse SEO data
// 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
// Our home page route, this returns src/pages/index.hbs with data built into it
fastify.get("/", function(request, reply) {
// params is an object we'll pass to our handlebars template
let params = { seo: seo };
// check and see if someone asked for a random color
// If someone clicked the option for a random color it'll be passed in the querystring
if (request.query.randomize) {
// we need to load our color data file, pick one at random, and add it to the params
// We need to load our color data file, pick one at random, and add it to the params
const colors = require("./src/colors.json");
const allColors = Object.keys(colors);
let currentColor = allColors[(allColors.length * Math.random()) << 0];
// Add the color properties to the params object
params = {
color: colors[currentColor],
colorError: null,
seo: seo
};
}
// The Handlebars code will be able to access the parameter values and build them into the page
reply.view("/src/pages/index.hbs", params);
});
// A POST route to handle and react to form submissions
fastify.post("/", function(request, reply) {
// Build the params object to pass to the template
let params = { seo: seo };
// the request.body.color is posted with a form submission
// If the user submitted a color through the form it'll be passed here in the request body
let color = request.body.color;
// if it's not empty, let's try to find the color
// If it's not empty, let's try to find the color
if (color) {
// load our color data file
// ADD CODE FROM README HERE TO SAVE SUBMITTED FAVORITES
// Load our color data file
const colors = require("./src/colors.json");
// take our form submission, remove whitespace, and convert to lowercase
// Take our form submission, remove whitespace, and convert to lowercase
color = color.toLowerCase().replace(/\s/g, "");
// now we see if that color is a key in our colors object
// Now we see if that color is a key in our colors object
if (colors[color]) {
// found one!
// Found one!
params = {
color: colors[color],
colorError: null,
seo: seo
};
} else {
// try again.
// No luck! Return the user value as the error property
params = {
colorError: request.body.color,
seo: seo
};
}
}
// The Handlebars template will use the parameter values to update the page with the chosen color
reply.view("/src/pages/index.hbs", params);
});