![](/img/trans.png)
[英]How to set custom HTTP response headers in Next.js without using a custom server?
[英]Next.js: middlewares without a custom server or wrappers
是否可以在不使用自定義服務器或包裝器處理程序的情況下使用中間件創建 Next.js 應用程序?
當我創建一個 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;
例如,. ./startup/cors
模塊包含以下行:
const cors = require("cors");
module.exports = function(app) {
app.use(cors());
};
但是,對於我的 Next.js 應用程序,我不明白如何在不創建自定義服務器的情況下獲得這樣的東西。
我已經在沒有自定義服務器的情況下遇到了在 Next.js 中使用中間件的文章,但它使用了我想避免的包裝器解決方案。
目前 Next.js 僅支持 api 路徑的中間件。 常規頁面路徑中尚不支持中間件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.