Skip to content

Instantly share code, notes, and snippets.

@juanchehin
Created July 5, 2023 14:33
Show Gist options
  • Save juanchehin/070769198ec7a05a270b42b0c58c1d52 to your computer and use it in GitHub Desktop.
Save juanchehin/070769198ec7a05a270b42b0c58c1d52 to your computer and use it in GitHub Desktop.
const sql = require('mssql');
/*
* Recieve a POST request to /users, with a user_id param on the request body.
*
* An SQL lookup will be performed, attempting to find a user in the database
* with the `id` provided in the `user_id` param.
*
* The result of the database query is sent back in the response.
*/
app.post('/users', function(req, res) {
const user_id = req.params.user_id;
/*
* Connect to the SQL database (server side).
*/
await sql.connect('mssql://username:password@localhost/database');
/*
* Query the database, providing the `user_id` param from the HTTP
* request body.
*/
const result = await sql.query('SELECT * FROM users WHERE USER = ' + user_id);
/*
* Return the result of the SQL query to the requester in the
* HTTP response.
*/
return res.json(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment