Loading

Quipoin Menu

Learn • Practice • Grow

express-js / MySQL/PostgreSQL with Express
interview

Q1. How do you connect Express to MySQL?
Install the mysql2 package. Create a connection pool: const mysql = require('mysql2'); const pool = mysql.createPool({ host:'localhost', user:'root', database:'test' }); Then use pool.promise() for async/await queries.

Q2. How do you connect Express to PostgreSQL?
Install the pg package. Create a pool: const { Pool } = require('pg'); const pool = new Pool({ user: 'postgres', host: 'localhost', database: 'mydb' }); Then use pool.query() for SQL queries.

Q3. What is an ORM/ODM and do you need one for SQL?
ORM (Object-Relational Mapping) like Sequelize or TypeORM provides a higher-level abstraction. You don't need one - you can write raw SQL. ORMs offer convenience, validation, and relationships but add complexity. Choose based on project needs.

Q4. How do you prevent SQL injection in Express?
Always use parameterized queries or prepared statements. With mysql2, use ? placeholders: pool.query('SELECT * FROM users WHERE id = ?', [userId]). Never concatenate user input directly into SQL strings. ORMs typically handle this automatically.

Q5. How do you handle transactions in SQL with Express?
Use connection pooling to get a connection, then BEGIN TRANSACTION, execute queries, and COMMIT or ROLLBACK. With mysql2/promise: const conn = await pool.getConnection(); await conn.beginTransaction(); try { await conn.query(...); await conn.commit(); } catch { await conn.rollback(); } finally { conn.release(); }