Facebook Pixel

Common Express Server Setup Mistakes (and How to Fix Them)

Express setups fail in predictable ways. Here are the common mistakes and fixes.

Common Express Server Setup Mistakes

Setting up Express wrong creates bugs late. Here are the common setup mistakes and how to fix them.

Mistake 1: Body Parser Missing or After Routes

You add app.use(express.json()) after your routes. req.body is undefined. Fix: add it before any route that reads req.body.

Mistake 2: No Error Handler

You forget the error handler. Errors crash the server or leak stack traces. Fix: register an error-handling middleware at the end of app.js with (err, req, res, next) => {}. Always send a safe message; log the full error internally.

Mistake 3: Hardcoded Port

You write app.listen(3000). Production uses a different port. Fix: read from env: app.listen(process.env.PORT || 3000).

Mistake 4: CORS Misconfigured

You forget CORS when client and server are cross-origin. The client gets blocked. Fix: install cors and add app.use(cors({ origin: 'https://yourclient.com', credentials: true })). Set credentials: true if you use cookies.

Mistake 5: No .env Loaded

You forget require('dotenv').config(). process.env.X is undefined in production. Fix: require dotenv at the top of server.js before anything that uses env vars.

Mistake 6: Routes Imported but Not Mounted

You create a router in routes/auth.js but forget to mount it: app.use('/auth', authRouter). The route 404s. Fix: mount every router in app.js.

Mistake 7: Handler Forgets to Respond

You do work in a handler but forget to send a response. The client hangs. Fix: every handler must call res.send, res.json, or res.end. Better: use a route wrapper that auto-catches errors.

Mistake 8: Handlers Calling next Without a Reason

Handlers call next() without meaning. Express moves to the next middleware (which may not exist). Fix: handlers either respond or call next(err) with an error. Not both. Not neither.

Mistake 9: sync Heavy Work in Handlers

You compute a hash or parse a huge file synchronously. The event loop blocks. Fix: use async APIs. For CPU-bound work, use worker threads or background queues.

Mistake 10: console.log in Production

You log with console.log. It is unstructured and slow. Fix: use a logger (winston, pino). Log JSON with level, timestamp, context. Ship to a central place.

The Takeaway

Common Express setup mistakes: body parser missing or misordered, no error handler, hardcoded port, CORS configured wrong, .env not loaded, routers not mounted, handlers that forget to respond, misuse of next, sync heavy work, and console.log in production. Fix them on day one.

Body parser missing or after routes, no error handler, hardcoded port, CORS misconfigured, .env not loaded, routers not mounted, handlers that forget to respond, misuse of next, sync heavy work in handlers, and console.log in production.

Because express.json() was not added or was added after the routes that read req.body. Add app.use(express.json()) before any route that needs to parse JSON request bodies.

Routers need to be mounted in app.js with app.use('/path', router). Creating a router in routes/auth.js is not enough; you must attach it to the Express app.

Your handler did the work but forgot to send a response. Every handler must call res.send, res.json, or res.end. Better: use a wrapper that catches errors and sends a response automatically.

Probably you forgot require('dotenv').config() at the top of server.js. Add it before any file that reads process.env. In production, env vars may be set by the host directly, but for local dev, dotenv is needed.

Ready to master Node.js completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.