【问题标题】:Next.js: middlewares without a custom server or wrappersNext.js:没有自定义服务器或包装器的中间件
【发布时间】:2020-08-07 08:16:10
【问题描述】:

是否可以在不使用自定义服务器或包装器处理程序的情况下使用中间件创建 Next.js 应用程序?

当我创建一个 Express 应用程序时,我将我的代码拆分为不同的 require 语句,调用 Express 中间件:

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;

例如,./startup/cors 模块包含以下行:

const cors = require("cors");

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

但是,对于我的 Next.js 应用程序,我不明白如何在不创建自定义服务器的情况下获得这样的东西。

我已经看到文章 Use middleware in Next.js without custom server,但它使用了我想避免的包装器解决方案。

【问题讨论】:

    标签: javascript node.js next.js


    【解决方案1】:

    目前 Next.js 仅支持 api 路径的 middlewares。常规页面路径中尚不支持中间件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-04
      • 2020-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 2020-01-18
      • 2020-10-05
      • 1970-01-01
      相关资源
      最近更新 更多