nathaner
nathaner

Reputation: 553

Next.js: middlewares without a custom server or wrappers

Is it possible to create a Next.js app with middlewares without using a custom server nor wrappers handlers?

When I create an Express app, I split my code into different require statements calling Express middlewares:

const express = require("express");
const app = express();

// I call the functions in each modules to use the different middlewares
require("./startup/cors")(app);
require("./startup/routes")(app);
require("./startup/db")();

const port = process.env.PORT || config.get("port");
const server = app.listen(port, () =>
  winston.info(`Listening on port ${port}...`)
);

module.exports = server;

For example, the ./startup/cors module contains the following lines:

const cors = require("cors");

module.exports = function(app) {
  app.use(cors());
};

However, with my Next.js app, I don't understand how to get something like this without creating a custom server.

I already came across the article Use middleware in Next.js without custom server but it uses a wrapper solution I would like to avoid.

Upvotes: 1

Views: 402

Answers (1)

felixmosh
felixmosh

Reputation: 35503

Currently Next.js supports middlewares only for api paths. There is no support for middlewares in regular pages paths yet.

Upvotes: 1

Related Questions