MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / httpGet

Function httpGet

cloud-sql/postgres/knex/index.js:209–253  ·  view source on GitHub ↗
(req, res)

Source from the content-addressed store, hash-verified

207};
208
209const httpGet = async (req, res) => {
210 pool = pool || (await createPoolAndEnsureSchema());
211 try {
212 // Query the total count of "TABS" from the database.
213 const tabsResult = await getVoteCount(pool, 'TABS');
214 const tabsTotalVotes = parseInt(tabsResult[0].count);
215 // Query the total count of "SPACES" from the database.
216 const spacesResult = await getVoteCount(pool, 'SPACES');
217 const spacesTotalVotes = parseInt(spacesResult[0].count);
218 // Query the last 5 votes from the database.
219 const votes = await getVotes(pool);
220 // Calculate and set leader values.
221 let leadTeam = '';
222 let voteDiff = 0;
223 let leaderMessage = '';
224 if (tabsTotalVotes !== spacesTotalVotes) {
225 if (tabsTotalVotes > spacesTotalVotes) {
226 leadTeam = 'TABS';
227 voteDiff = tabsTotalVotes - spacesTotalVotes;
228 } else {
229 leadTeam = 'SPACES';
230 voteDiff = spacesTotalVotes - tabsTotalVotes;
231 }
232 leaderMessage =
233 `${leadTeam} are winning by ${voteDiff} vote` +
234 (voteDiff > 1 ? 's' : '');
235 } else {
236 leaderMessage = 'TABS and SPACES are evenly matched!';
237 }
238 res.render('index.pug', {
239 votes: votes,
240 tabsCount: tabsTotalVotes,
241 spacesCount: spacesTotalVotes,
242 leadTeam: leadTeam,
243 voteDiff: voteDiff,
244 leaderMessage: leaderMessage,
245 });
246 } catch (err) {
247 console.error(err);
248 res
249 .status(500)
250 .send('Unable to load page; see logs for more details.')
251 .end();
252 }
253};
254
255app.get('/', httpGet);
256

Callers 3

index.jsFile · 0.70
index.jsFile · 0.50
index.jsFile · 0.50

Calls 4

getVoteCountFunction · 0.70
getVotesFunction · 0.70
renderMethod · 0.45

Tested by

no test coverage detected