Import of glitchdotcom/glitch-hello-node
This commit is contained in:
1 parent
5caccbf885
commit
4e508b417c
6 files changed
+768
-81
No files matched your search
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
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
|
||||
/**
|
||||
* This is the main Node.js server script for your project
|
||||
* Check out the two endpoints this back-end API provides in fastify.get and fastify.post below
|
||||
*/
|
||||
|
||||
const path = require("path");
|
||||
@@ -11,7 +11,7 @@ const fastify = require("fastify")({
|
||||
logger: false
|
||||
});
|
||||
|
||||
// ADD FAVORITES ARRAY VARIABLE FROM README HERE
|
||||
// ADD FAVORITES ARRAY VARIABLE FROM TODO HERE
|
||||
|
||||
|
||||
// Setup our static files
|
||||
@@ -36,16 +36,24 @@ if (seo.url === "glitch-default") {
|
||||
seo.url = `https://${process.env.PROJECT_DOMAIN}.glitch.me`;
|
||||
}
|
||||
|
||||
// Our home page route, this returns src/pages/index.hbs with data built into it
|
||||
/**
|
||||
* Our home page route
|
||||
*
|
||||
* 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 };
|
||||
|
||||
// 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
|
||||
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],
|
||||
@@ -53,26 +61,37 @@ fastify.get("/", function(request, reply) {
|
||||
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
|
||||
/**
|
||||
* Our POST route to handle and react to form submissions
|
||||
*
|
||||
* Accepts body data indicating the user choice
|
||||
*/
|
||||
fastify.post("/", function(request, reply) {
|
||||
|
||||
// Build the params object to pass to the template
|
||||
let params = { seo: seo };
|
||||
|
||||
// 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 (color) {
|
||||
// ADD CODE FROM README HERE TO SAVE SUBMITTED FAVORITES
|
||||
// ADD CODE FROM TODO 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
|
||||
color = color.toLowerCase().replace(/\s/g, "");
|
||||
|
||||
// Now we see if that color is a key in our colors object
|
||||
if (colors[color]) {
|
||||
|
||||
// Found one!
|
||||
params = {
|
||||
color: colors[color],
|
||||
@@ -80,6 +99,7 @@ fastify.post("/", function(request, reply) {
|
||||
seo: seo
|
||||
};
|
||||
} else {
|
||||
|
||||
// No luck! Return the user value as the error property
|
||||
params = {
|
||||
colorError: request.body.color,
|
||||
@@ -87,6 +107,7 @@ fastify.post("/", function(request, reply) {
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// The Handlebars template will use the parameter values to update the page with the chosen color
|
||||
reply.view("/src/pages/index.hbs", params);
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user