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

Function httpPost

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

Source from the content-addressed store, hash-verified

255app.get('/', httpGet);
256
257const httpPost = async (req, res) => {
258 pool = pool || (await createPoolAndEnsureSchema());
259 // Get the team from the request and record the time of the vote.
260 const {team} = req.body;
261 const timestamp = new Date();
262
263 if (!team || (team !== 'TABS' && team !== 'SPACES')) {
264 res.status(400).send('Invalid team specified.').end();
265 return;
266 }
267
268 // Create a vote record to be stored in the database.
269 const vote = {
270 candidate: team,
271 time_cast: timestamp,
272 };
273
274 // Save the data to the database.
275 try {
276 await insertVote(pool, vote);
277 } catch (err) {
278 logger.error(`Error while attempting to submit vote:${err}`);
279 res
280 .status(500)
281 .send('Unable to cast vote; see logs for more details.')
282 .end();
283 return;
284 }
285 res.status(200).send(`Successfully voted for ${team} at ${timestamp}`).end();
286};
287
288app.post('*', httpPost);
289

Callers 3

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

Calls 2

insertVoteFunction · 0.70

Tested by

no test coverage detected