Loading

Quipoin Menu

Learn • Practice • Grow

express-js / express-js - interview
interview

Q1. What is express.json() middleware?
express.json() is a built-in middleware that parses incoming requests with JSON payloads. It populates req.body with the parsed JSON data. It's essential for handling POST/PUT requests that send JSON. Example: app.use(express.json()).

Q2. What is express.urlencoded() middleware?
express.urlencoded() parses incoming requests with URL-encoded payloads (from HTML forms). It populates req.body with key-value pairs. You typically use { extended: true } option to allow rich objects and arrays. Example: app.use(express.urlencoded({ extended: true })).

Q3. What is express.static() middleware?
express.static() serves static files like images, CSS, JavaScript files. You pass the directory name. Example: app.use(express.static('public')). Now files in the public folder are accessible directly (e.g., http://localhost:3000/style.css). You can also use multiple static directories.

Q4. What's the difference between express.json() and express.urlencoded()?
express.json() parses JSON Content-Type (application/json) requests, while express.urlencoded() parses application/x-www-form-urlencoded (HTML form submissions). Both populate req.body but handle different content types. Often you need both in your app.

Q5. Do you need body-parser package anymore?
No, Express 4.16+ includes built-in body parsing middleware. Previously, body-parser was a separate package. Now you can use express.json() and express.urlencoded() directly. These are essentially the same as body-parser's json and urlencoded methods.