简体   繁体   English

Next.js:没有自定义服务器或包装器的中间件

[英]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?是否可以在不使用自定义服务器或包装器处理程序的情况下使用中间件创建 Next.js 应用程序?

When I create an Express app, I split my code into different require statements calling Express middlewares:当我创建一个 Express 应用程序时,我将我的代码拆分为调用 Express 中间件的不同 require 语句:

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:例如,. ./startup/cors模块包含以下行:

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

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.我已经在没有自定义服务器的情况下遇到了在 Next.js 中使用中间件的文章,但它使用了我想避免的包装器解决方案。

Currently Next.js supports middlewares only for api paths.目前 Next.js 仅支持 api 路径的中间件 There is no support for middlewares in regular pages paths yet.常规页面路径中尚不支持中间件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM