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

Function httpPost

cloud-sql/sqlserver/tedious/index.js:158–188  ·  view source on GitHub ↗
(req, res)

Source from the content-addressed store, hash-verified

156
157// Handle incoming vote requests and inserting them into the database.
158const httpPost = async (req, res) => {
159 const {team} = req.body;
160 const timestamp = new Date();
161
162 if (!team || (team !== 'TABS' && team !== 'SPACES')) {
163 return res.status(400).send('Invalid team specified.').end();
164 }
165
166 try {
167 const stmt =
168 'INSERT INTO votes (time_cast, candidate) VALUES (@timestamp, @team)';
169 const {DateTime, NVarChar} = connection.TYPES;
170 // Runs query
171 await connection.query(stmt, [
172 ['timestamp', DateTime, timestamp],
173 ['team', NVarChar, team],
174 ]);
175 } catch (err) {
176 // If something goes wrong, handle the error in this section. This might
177 // involve retrying or adjusting parameters depending on the situation.
178 logger.error(err);
179 return res
180 .status(500)
181 .send(
182 'Unable to successfully cast vote! Please check the application logs for more details.'
183 )
184 .end();
185 }
186
187 res.status(200).send(`Successfully voted for ${team} at ${timestamp}`).end();
188};
189
190app.post('*', httpPost);
191

Callers 1

index.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected