0
0
ExpressHow-ToBeginner · 3 min read

How to Use Controller in Express: Simple Guide

In Express, a controller is a module that handles the logic for routes. You create controller functions to separate route handling from business logic, then import and use them in your route definitions with app.get, app.post, etc.
📐

Syntax

A controller in Express is usually a JavaScript file exporting functions that handle requests and responses. You import these functions in your route file and assign them as callbacks for routes.

  • exports.functionName = (req, res) => { ... }: Defines a controller function.
  • const controller = require('./controller'): Imports the controller module.
  • app.get('/path', controller.functionName): Uses the controller function for a route.
javascript
exports.home = (req, res) => {
  res.send('Hello from controller!');
};

// In routes file
const express = require('express');
const router = express.Router();
const controller = require('./controller');

router.get('/', controller.home);

module.exports = router;
💻

Example

This example shows a simple Express app using a controller to handle the home page route. The controller function sends a greeting message.

javascript
const express = require('express');
const app = express();

// Controller module
const homeController = {
  home: (req, res) => {
    res.send('Welcome to the Home Page!');
  }
};

// Route using controller
app.get('/', homeController.home);

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

Common Pitfalls

Common mistakes when using controllers in Express include:

  • Not exporting controller functions properly, causing undefined errors.
  • Mixing route logic and controller logic in the same file, which makes code hard to maintain.
  • Forgetting to call next() in middleware-style controllers when needed.

Always keep controllers focused on handling requests and responses, and export them correctly.

javascript
/* Wrong: Controller function not exported */
// controller.js
const home = (req, res) => {
  res.send('Hello');
};

// routes.js
const express = require('express');
const app = express();
const controller = require('./controller');
app.get('/', controller.home); // Error: home is undefined

/* Right: Export controller function */
// controller.js
exports.home = (req, res) => {
  res.send('Hello');
};
📊

Quick Reference

  • Define controller functions in separate files and export them.
  • Import controllers in your route files.
  • Use controller functions as callbacks in app.get, app.post, etc.
  • Keep controllers focused on request handling, not routing setup.

Key Takeaways

Controllers separate route logic from business logic for cleaner code.
Always export controller functions properly to avoid undefined errors.
Import controllers in route files and use them as route handlers.
Keep controllers focused on handling requests and responses only.
Organizing code with controllers improves maintainability and clarity.