0
0
Expressframework~10 mins

Why SQL integration matters in Express - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the SQL library in an Express app.

Express
const sql = require('[1]');
Drag options to blanks, or click blank then click option'
Amysql
Bfs
Chttp
Dexpress
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'express' instead of the SQL library name.
Using 'http' or 'fs' which are unrelated to SQL.
2fill in blank
medium

Complete the code to create a connection to the SQL database.

Express
const connection = sql.createConnection({ host: 'localhost', user: 'root', password: '[1]', database: 'testdb' });
Drag options to blanks, or click blank then click option'
A1234
Bpassword123
Cexpress
Droot
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'root' as password instead of the actual password.
Using 'express' which is unrelated.
3fill in blank
hard

Fix the error in the query execution code by completing the blank.

Express
connection.query('SELECT * FROM users', function(err, results) { if (err) throw [1]; console.log(results); });
Drag options to blanks, or click blank then click option'
Aerror
Bexception
Cerr
Dresults
Attempts:
3 left
💡 Hint
Common Mistakes
Throwing 'error' which is undefined here.
Throwing 'results' which is the data, not the error.
4fill in blank
hard

Fill both blanks to create a simple Express route that queries the database and sends results.

Express
app.get('/users', (req, res) => { connection.query('[1]', (err, results) => { if (err) return res.status(500).send(err); res.[2](results); }); });
Drag options to blanks, or click blank then click option'
ASELECT * FROM users
Bsend
Cjson
DSELECT * FROM products
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong table name in the query.
Using res.send() which sends raw data, not JSON.
5fill in blank
hard

Fill all three blanks to handle a POST request that inserts a new user into the database.

Express
app.post('/add-user', (req, res) => { const { name, email } = req.body; const query = 'INSERT INTO users (name, email) VALUES (?, ?)'; connection.query(query, [[1], [2]], (err, results) => { if (err) return res.status(500).send(err); res.[3]({ message: 'User added', id: results.insertId }); }); });
Drag options to blanks, or click blank then click option'
Aname
Bemail
Cjson
Dres.send
Attempts:
3 left
💡 Hint
Common Mistakes
Passing wrong variables to the query parameters.
Using res.send instead of res.json.