Node.js Connect and Express: Exploring Use Cases and Applications

If you’re starting a web server in Node.js, you’ll come across both Connect and Express, and it isn’t obvious how they relate. Both are frameworks that pass each request through middleware, small functions that run in order, while Express also maps each URL and method to the code that handles it.

I’ll run the same middleware in both frameworks to show where Express’s routing changes your code.

What Node.js, Connect, and Express mean

Node.js provides the JavaScript runtime and built-in HTTP APIs. Connect adds an ordered middleware stack, while Express adds routing and middleware.

Connect is a separate middleware framework for Node.js HTTP servers.

Both Connect and Express use middleware, but Express 4 removed its dependency on Connect. Think of them as two frameworks that share a request-handling style, not as a stack where Connect sits underneath Express.

TechnologyWhat it providesWhere it fits
Node.jsRuntime and built-in HTTP server APIFoundation for a server
ConnectOrdered middleware stackStandalone HTTP middleware framework
ExpressRouting plus middlewareWeb apps and APIs

The practical distinction is routing. Connect’s app receives a request through its middleware stack, while Express also matches a method and path to a route handler.

How middleware moves a request

Middleware is a function that runs while a request is being handled, with access to request, response, and next in Express.

app.use((req, res, next) => {
  res.setHeader('X-Request-Stage', 'middleware-ran');
  next();
});

Order matters because each function either ends the response or hands control onward. A function that does neither leaves the request waiting.

Express handles routes and middleware

The example uses the Express and Connect packages installed in a Node.js project. Save the code as article-sample.js in that project, then run node article-sample.js.

I ran the Express and Connect handlers against local HTTP requests. Each returned status 200, the middleware header, and its own response body.

Terminal output showing Express and Connect each returning an HTTP response after middleware runs
Both handlers return a response after their middleware passes control.
const express = require('express');
const connect = require('connect');
const http = require('node:http');
const app = express();
app.use((req, res, next) => {
  res.setHeader('X-Request-Stage', 'middleware-ran');
  next();
});
app.get('/hello', (req, res) => res.send('Hello from Express'));
const server = app.listen(0, () => {
  http.get(`http://127.0.0.1:${server.address().port}/hello`, res => {
    let body = '';
    res.on('data', chunk => body += chunk);
    res.on('end', () => {
      console.log('Express:', res.statusCode, res.headers['x-request-stage'], body);
      server.close();
      runConnect();
    });
  });
});
function runConnect() {
  const app = connect();
  app.use((req, res, next) => {
    res.setHeader('X-Request-Stage', 'middleware-ran');
    next();
  });
  app.use((req, res) => res.end('Hello from Connect'));
  const server = http.createServer(app).listen(0, () => {
    http.get(`http://127.0.0.1:${server.address().port}/hello`, res => {
      let body = '';
      res.on('data', chunk => body += chunk);
      res.on('end', () => {
        console.log('Connect:', res.statusCode, res.headers['x-request-stage'], body);
        server.close();
      });
    });
  });
}

The client request starts after the server is listening, and server.close() frees its socket when the response ends.

Connect exposes a middleware stack

Connect’s app is a request-listener function that you can pass to node:http’s createServer(). Each app.use() call adds a middleware function to the stack.

QuestionConnectExpress
How does a request enter?Through the middleware stackThrough the app’s middleware and routing layers
How does it choose a route?Middleware can inspect the request or mount pathRoute methods match HTTP method and path
Does Express depend on Connect?Not applicableNo, Express 4 removed that dependency

Choose Express when your app needs its routing API. Adding Connect underneath Express is not a prerequisite.

When a middleware leaves a request hanging

Every middleware path must either send a response or pass control onward. If a conditional branch returns without calling next() or ending the response, the client keeps waiting.

  • Use next() when another middleware or route should handle the request.
  • Send or end the response when the current handler has the final result.

Error-handling middleware follows a separate Express signature with four arguments, (err, req, res, next). Keep it after the middleware and routes whose errors it should handle.

Choose the abstraction that matches the job

Use Node.js HTTP when you want to build the request handling yourself, Connect when an ordered middleware stack is the task, and Express when route matching belongs in the framework. Express and Connect are separate choices, even though their middleware functions use a similar handoff model.

node package/server.js

What is Connect in Node.js?

Connect is a Node.js middleware framework that composes request handlers into an ordered stack.

Does Express use Connect?

Express 4 removed its dependency on Connect. Express has its own routing and middleware framework.

What does middleware next() do?

Calling next() passes control to the next middleware or matching handler. A middleware must also end the response when it has the final result.

Divya Maddipati
Divya Maddipati

Divya Maddipati writes introductory programming guides across Python and Node.js. Her articles cover NumPy, Python modules, and Connect and Express.

Articles: 3