Skip to main content
Two module systems exist in Node.js: the original CommonJS (ES5 style) and the modern ES Modules (ES6 style). You will encounter both in real projects. This page builds the same Express server in each style, highlights every syntax difference, and covers the ES6 features you use daily in backend development.

ES5 vs ES6: Syntax Comparison Table

CommonJS is the default in Node.js. ES Modules need "type": "module" in package.json. You cannot mix them in the same file.

Install Express

Express Server: ES5 (CommonJS)

Express Server: ES6 (ES Modules)

First add to package.json:

Side-by-Side Comparison

Core Express Concepts

The Request-Response Cycle

1

Client sends request

Browser or mobile app sends HTTP request to a URL with a method (GET, POST…).
2

Express matches route

Express checks registered routes for a matching path and method.
3

Middleware runs

Each registered app.use() function runs in order before the route handler.
4

Route handler executes

The matched handler runs business logic and queries the database.
5

Response sent

Handler calls res.json(), res.send(), or res.status().json() to respond.

Essential Express Methods

req and res Objects

ES6 Features Used Daily in Node.js

Arrow Functions

Template Literals

Destructuring

Spread Operator

async/await

async/await is syntactic sugar over Promises. An async function always returns a Promise. await pauses execution until the Promise resolves. Always wrap await in try/catch.

Error Handling in Express

Centralized Error Handler (must be LAST)

Passing Errors from Async Routes

Key Terms

Common Mistakes

Once you add "type": "module", all .js files must use import. If one file uses require(), rename it to .cjs.
Without app.use(express.json()), req.body is undefined for POST and PUT requests. This is the most common beginner error.
Always return res.status(404).json(...) to exit the function. Without return, Express tries to send two responses and crashes with “headers already sent.”
Error handling middleware must have exactly 4 parameters: (err, req, res, next). With 3 params, Express treats it as a normal middleware and errors bypass it.