Express.js Cheatsheet (2025) for Job Interview Preparation

Express.js, often referred to as the de facto standard for Node.js web frameworks, continues to dominate the server-side JavaScript ecosystem in 2025. Its simplicity, flexibility, and extensive middleware ecosystem make it a favorite among developers for creating robust, scalable web applications. But what makes it so compelling today? Express.js offers a lightweight, unopinionated architecture, making it an excellent choice for everything from rapid prototyping to building enterprise-grade APIs. Let’s dive into how you can master Express.js with this comprehensive cheatsheet, packed with examples for all skill levels.


How Do You Set Up an Express.js Project?

Setting up an Express.js project is quick and straightforward. Start by ensuring you have Node.js installed on your system. Then, create a new project directory, navigate into it, and initialize your project:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
mkdir express-app && cd express-app
npm init -y
mkdir express-app && cd express-app npm init -y
mkdir express-app && cd express-app
npm init -y

Install Express.js:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install express
npm install express
npm install express

Here’s a basic example of setting up a server:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Welcome to Express.js!');
});
app.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});
const express = require('express'); const app = express(); app.get('/', (req, res) => { res.send('Welcome to Express.js!'); }); app.listen(3000, () => { console.log('Server is running on http://localhost:3000'); });
const express = require('express');
const app = express();

app.get('/', (req, res) => {
    res.send('Welcome to Express.js!');
});

app.listen(3000, () => {
    console.log('Server is running on http://localhost:3000');
});

This code sets up a simple server that listens on port 3000 and responds to GET requests to the root URL with a welcome message.


How Can You Use Middleware in Express.js?

Middleware functions are the backbone of Express.js. They process requests and responses in the application pipeline. Middleware can be built-in, third-party, or custom.

Example: Logging Middleware (Easy)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.use((req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
app.use((req, res, next) => { console.log(`${req.method} ${req.url}`); next(); });
app.use((req, res, next) => {
    console.log(`${req.method} ${req.url}`);
    next();
});

This middleware logs the HTTP method and URL of every incoming request.

Example: Using Third-Party Middleware (Intermediate)

Install morgan for logging:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install morgan
npm install morgan
npm install morgan

Integrate it into your app:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const morgan = require('morgan');
app.use(morgan('tiny'));
const morgan = require('morgan'); app.use(morgan('tiny'));
const morgan = require('morgan');
app.use(morgan('tiny'));

Example: Custom Error-Handling Middleware (Expert)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send('Something broke!');
});
app.use((err, req, res, next) => { console.error(err.stack); res.status(500).send('Something broke!'); });
app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send('Something broke!');
});

This custom middleware catches errors and sends a generic error message to the client.


What Are Routes in Express.js, and How Do You Define Them?

Routes are how Express.js maps HTTP requests to handler functions.

Example: Basic Route (Easy)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.get('/hello', (req, res) => {
res.send('Hello, World!');
});
app.get('/hello', (req, res) => { res.send('Hello, World!'); });
app.get('/hello', (req, res) => {
    res.send('Hello, World!');
});

Example: Route Parameters (Intermediate)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.get('/users/:id', (req, res) => {
const userId = req.params.id;
res.send(`User ID: ${userId}`);
});
app.get('/users/:id', (req, res) => { const userId = req.params.id; res.send(`User ID: ${userId}`); });
app.get('/users/:id', (req, res) => {
    const userId = req.params.id;
    res.send(`User ID: ${userId}`);
});

Example: Route Grouping with Router (Expert)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const userRouter = express.Router();
userRouter.get('/', (req, res) => {
res.send('User List');
});
userRouter.get('/:id', (req, res) => {
res.send(`User Details for ID: ${req.params.id}`);
});
app.use('/users', userRouter);
const userRouter = express.Router(); userRouter.get('/', (req, res) => { res.send('User List'); }); userRouter.get('/:id', (req, res) => { res.send(`User Details for ID: ${req.params.id}`); }); app.use('/users', userRouter);
const userRouter = express.Router();

userRouter.get('/', (req, res) => {
    res.send('User List');
});

userRouter.get('/:id', (req, res) => {
    res.send(`User Details for ID: ${req.params.id}`);
});

app.use('/users', userRouter);

Here, the userRouter groups all routes related to users, making the application more modular.


How Can You Handle Form Data in Express.js?

Handling form data is crucial for many web applications. Express.js provides built-in middleware for parsing incoming requests.

Example: Parsing URL-Encoded Data (Easy)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.use(express.urlencoded({ extended: true }));
app.post('/submit', (req, res) => {
res.send(`Hello, ${req.body.name}`);
});
app.use(express.urlencoded({ extended: true })); app.post('/submit', (req, res) => { res.send(`Hello, ${req.body.name}`); });
app.use(express.urlencoded({ extended: true }));

app.post('/submit', (req, res) => {
    res.send(`Hello, ${req.body.name}`);
});

Example: Parsing JSON Data (Intermediate)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.use(express.json());
app.post('/api/data', (req, res) => {
res.json({ received: req.body });
});
app.use(express.json()); app.post('/api/data', (req, res) => { res.json({ received: req.body }); });
app.use(express.json());

app.post('/api/data', (req, res) => {
    res.json({ received: req.body });
});

Example: File Uploads with Multer (Expert)

Install Multer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install multer
npm install multer
npm install multer

Use Multer for file uploads:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
app.post('/upload', upload.single('file'), (req, res) => {
res.send(`File uploaded: ${req.file.originalname}`);
});
const multer = require('multer'); const upload = multer({ dest: 'uploads/' }); app.post('/upload', upload.single('file'), (req, res) => { res.send(`File uploaded: ${req.file.originalname}`); });
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });

app.post('/upload', upload.single('file'), (req, res) => {
    res.send(`File uploaded: ${req.file.originalname}`);
});

How Do You Implement Authentication in Express.js?

Authentication is a critical part of most web applications. Express.js supports various methods of implementing it.

Example: Basic Authentication (Easy)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.use((req, res, next) => {
const auth = req.headers.authorization;
if (auth === 'Basic dXNlcjpwYXNz') { // base64 for 'user:pass'
next();
} else {
res.status(401).send('Unauthorized');
}
});
app.use((req, res, next) => { const auth = req.headers.authorization; if (auth === 'Basic dXNlcjpwYXNz') { // base64 for 'user:pass' next(); } else { res.status(401).send('Unauthorized'); } });
app.use((req, res, next) => {
    const auth = req.headers.authorization;
    if (auth === 'Basic dXNlcjpwYXNz') { // base64 for 'user:pass'
        next();
    } else {
        res.status(401).send('Unauthorized');
    }
});

Example: Using Passport.js for Authentication (Intermediate)

Install Passport.js:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install passport passport-local
npm install passport passport-local
npm install passport passport-local

Set up Passport:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy((username, password, done) => {
if (username === 'admin' && password === 'password') {
return done(null, { id: 1, name: 'Admin' });
} else {
return done(null, false);
}
}));
app.use(passport.initialize());
app.post('/login', passport.authenticate('local', {
successRedirect: '/dashboard',
failureRedirect: '/login'
}));
const passport = require('passport'); const LocalStrategy = require('passport-local').Strategy; passport.use(new LocalStrategy((username, password, done) => { if (username === 'admin' && password === 'password') { return done(null, { id: 1, name: 'Admin' }); } else { return done(null, false); } })); app.use(passport.initialize()); app.post('/login', passport.authenticate('local', { successRedirect: '/dashboard', failureRedirect: '/login' }));
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;

passport.use(new LocalStrategy((username, password, done) => {
    if (username === 'admin' && password === 'password') {
        return done(null, { id: 1, name: 'Admin' });
    } else {
        return done(null, false);
    }
}));

app.use(passport.initialize());

app.post('/login', passport.authenticate('local', {
    successRedirect: '/dashboard',
    failureRedirect: '/login'
}));

Example: JWT Authentication (Expert)

Install jsonwebtoken:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install jsonwebtoken
npm install jsonwebtoken
npm install jsonwebtoken

Implement JWT-based authentication:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const jwt = require('jsonwebtoken');
const secretKey = 'your_secret_key';
app.post('/login', (req, res) => {
const token = jwt.sign({ username: req.body.username }, secretKey, { expiresIn: '1h' });
res.json({ token });
});
app.get('/protected', (req, res) => {
const token = req.headers['authorization'];
if (!token) return res.status(401).send('Access Denied');
try {
const verified = jwt.verify(token, secretKey);
res.json({ message: 'Welcome to the protected route!', user: verified });
} catch (err) {
res.status(400).send('Invalid Token');
}
});
const jwt = require('jsonwebtoken'); const secretKey = 'your_secret_key'; app.post('/login', (req, res) => { const token = jwt.sign({ username: req.body.username }, secretKey, { expiresIn: '1h' }); res.json({ token }); }); app.get('/protected', (req, res) => { const token = req.headers['authorization']; if (!token) return res.status(401).send('Access Denied'); try { const verified = jwt.verify(token, secretKey); res.json({ message: 'Welcome to the protected route!', user: verified }); } catch (err) { res.status(400).send('Invalid Token'); } });
const jwt = require('jsonwebtoken');
const secretKey = 'your_secret_key';

app.post('/login', (req, res) => {
    const token = jwt.sign({ username: req.body.username }, secretKey, { expiresIn: '1h' });
    res.json({ token });
});

app.get('/protected', (req, res) => {
    const token = req.headers['authorization'];
    if (!token) return res.status(401).send('Access Denied');

    try {
        const verified = jwt.verify(token, secretKey);
        res.json({ message: 'Welcome to the protected route!', user: verified });
    } catch (err) {
        res.status(400).send('Invalid Token');
    }
});

How Can You Optimize Performance in Express.js?

Performance optimization ensures your app can handle a growing number of users efficiently.

Example: Compression Middleware (Easy)

Install and use compression:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install compression
npm install compression
npm install compression
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const compression = require('compression');
app.use(compression());
const compression = require('compression'); app.use(compression());
const compression = require('compression');
app.use(compression());

Example: Serving Static Files Efficiently (Intermediate)

Express provides a built-in method to serve static files quickly using the express.static middleware. Here's an example:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.use(express.static('public'));
// Serve a static file directly
app.get('/about', (req, res) => {
res.sendFile(__dirname + '/public/about.html');
});
app.use(express.static('public')); // Serve a static file directly app.get('/about', (req, res) => { res.sendFile(__dirname + '/public/about.html'); });
app.use(express.static('public'));

// Serve a static file directly
app.get('/about', (req, res) => {
    res.sendFile(__dirname + '/public/about.html');
});

Place your static files (HTML, CSS, JS, images) in a folder named public. This middleware serves them efficiently, bypassing the need for manual route handling.

Example: Leveraging Cache-Control for Static Assets (Expert)

To further improve the performance of serving static files, use cache control:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.use(express.static('public', {
maxAge: '1d', // Cache static files for 1 day
etag: false // Disable etag for better performance
}));
app.use(express.static('public', { maxAge: '1d', // Cache static files for 1 day etag: false // Disable etag for better performance }));
app.use(express.static('public', {
    maxAge: '1d', // Cache static files for 1 day
    etag: false   // Disable etag for better performance
}));

This allows browsers to cache static assets, reducing server load and improving response times.

Example: Advanced SEO and Performance Hacks

Compression Headers

Use the compression middleware to ensure that responses are sent with gzip or Brotli compression, making them smaller and faster to transmit. Additionally, set up headers:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.use((req, res, next) => {
res.setHeader('Content-Encoding', 'gzip');
next();
});
app.use((req, res, next) => { res.setHeader('Content-Encoding', 'gzip'); next(); });
app.use((req, res, next) => {
    res.setHeader('Content-Encoding', 'gzip');
    next();
});

Cache Control for API Responses

For dynamic content, use proper Cache-Control headers to enhance caching while maintaining fresh data:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
app.get('/api/data', (req, res) => {
res.set('Cache-Control', 'public, max-age=300'); // Cache for 5 minutes
res.json({ data: 'Cached API Response' });
});
app.get('/api/data', (req, res) => { res.set('Cache-Control', 'public, max-age=300'); // Cache for 5 minutes res.json({ data: 'Cached API Response' }); });
app.get('/api/data', (req, res) => {
    res.set('Cache-Control', 'public, max-age=300'); // Cache for 5 minutes
    res.json({ data: 'Cached API Response' });
});

Server Tuning

Optimize your server for high performance by tweaking Node.js settings and deploying on a high-performance hosting platform. Use process managers like PM2 to monitor and scale applications.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
pm install pm2 -g
pm2 start app.js -i max --watch
pm install pm2 -g pm2 start app.js -i max --watch
pm install pm2 -g
pm2 start app.js -i max --watch

This configuration allows you to utilize all CPU cores and automatically restart on file changes.

Example: Caching with Memcached (Intermediate)

Install Memcached client:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install memcached
npm install memcached
npm install memcached

Set up caching:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const Memcached = require('memcached');
const memcached = new Memcached('localhost:11211');
app.get('/data', (req, res) => {
memcached.get('key', (err, data) => {
if (data) {
return res.json(JSON.parse(data));
}
const freshData = { message: 'Fresh Data' };
memcached.set('key', JSON.stringify(freshData), 3600, (err) => {
if (err) console.error(err);
});
res.json(freshData);
});
});
const Memcached = require('memcached'); const memcached = new Memcached('localhost:11211'); app.get('/data', (req, res) => { memcached.get('key', (err, data) => { if (data) { return res.json(JSON.parse(data)); } const freshData = { message: 'Fresh Data' }; memcached.set('key', JSON.stringify(freshData), 3600, (err) => { if (err) console.error(err); }); res.json(freshData); }); });
const Memcached = require('memcached');
const memcached = new Memcached('localhost:11211');

app.get('/data', (req, res) => {
    memcached.get('key', (err, data) => {
        if (data) {
            return res.json(JSON.parse(data));
        }

        const freshData = { message: 'Fresh Data' };
        memcached.set('key', JSON.stringify(freshData), 3600, (err) => {
            if (err) console.error(err);
        });

        res.json(freshData);
    });
});

Example: Load Balancing (Expert)

Use a reverse proxy like NGINX or PM2 for load balancing. With PM2, you can enable clustering easily:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install pm2 -g
pm2 start app.js -i max
npm install pm2 -g pm2 start app.js -i max
npm install pm2 -g
pm2 start app.js -i max

This spins up multiple instances of your app to utilize multi-core processors.


How Do You Test and Debug an Express.js Application?

Testing and debugging ensure your application works as intended.

Example: Logging with Debug (Easy)

Install the debug package:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install debug
npm install debug
npm install debug

Use it in your app:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const debug = require('debug')('app');
app.get('/', (req, res) => {
debug('Handling root route');
res.send('Debugging Express.js');
});
const debug = require('debug')('app'); app.get('/', (req, res) => { debug('Handling root route'); res.send('Debugging Express.js'); });
const debug = require('debug')('app');
app.get('/', (req, res) => {
    debug('Handling root route');
    res.send('Debugging Express.js');
});

Example: Writing Unit Tests with Jest (Intermediate)

Install Jest:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npm install jest supertest --save-dev
npm install jest supertest --save-dev
npm install jest supertest --save-dev

Create a test file:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
const request = require('supertest');
const app = require('../app'); // your Express app
describe('GET /', () => {
it('should return a welcome message', async () => {
const response = await request(app).get('/');
expect(response.status).toBe(200);
expect(response.text).toBe('Welcome to Express.js!');
});
});
const request = require('supertest'); const app = require('../app'); // your Express app describe('GET /', () => { it('should return a welcome message', async () => { const response = await request(app).get('/'); expect(response.status).toBe(200); expect(response.text).toBe('Welcome to Express.js!'); }); });
const request = require('supertest');
const app = require('../app'); // your Express app

describe('GET /', () => {
    it('should return a welcome message', async () => {
        const response = await request(app).get('/');
        expect(response.status).toBe(200);
        expect(response.text).toBe('Welcome to Express.js!');
    });
});

Run the tests:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
npx jest
npx jest
npx jest

Example: Debugging with VS Code (Expert)

Set up a launch configuration in launch.json:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Express",
"program": "${workspaceFolder}/app.js"
}
]
}
{ "version": "0.2.0", "configurations": [ { "type": "node", "request": "launch", "name": "Debug Express", "program": "${workspaceFolder}/app.js" } ] }
{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Debug Express",
            "program": "${workspaceFolder}/app.js"
        }
    ]
}

Run the debugger, set breakpoints, and inspect your application’s runtime.

Total
0
Shares
Previous Post

How to Secure Your Hosted JSON File: Simple and Effective Methods

Next Post

Is Your Next.js Build Manifest Exposing Site Vulnerabilities?

Related Posts