如何在Heroku上的两个不同端口上同时部署Next.js和Express服务器

x8goxv8g  于 2022-11-13  发布在  其他
关注(0)|答案(1)|浏览(189)

这 是 我 目前 面临 的 部署 挑战 。 我 在 谷歌 上 搜索 了 相当 多 的 内容 , 尝试 了 一些 , 但 没有 成功 。
我 有 一 个 应用 程序 A , 它 使用 express 作为 服务 器 , 在 端口 5000 提供 服务 。 然后 我 有 一 个 应用 程序 B , 它 使用 Next 作为 服务 器 , 在 端口 3000 提供 服务 。 我 写 了 一些 代码 , 将 两 个 应用 程序 集成 为 一 个 , 我 试图 将 其 部署 到 Heroku 上 。 我 一直 遇到 部署 失败 。 下面 是 我 的 package.json :

"scripts": {
"start": "concurrently \" npm run dev \" \" next \" ",
....

中 的 每 一 个

hrirmatl

hrirmatl1#

或者,您可以使用Next.js的Custom Server functionality,只需首先定义您的API路由,然后将expressApp.all('*', nextHandler)放在末尾,以便Next捕获您的API端点未捕获的所有内容。

const express = require('express');
const next = require('next');

const APIRouter = require('./routes');

const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = 3000;

const nextApp = next({dev, hostname, port});
const nextHandler = nextApp.getRequestHandler();

const expressApp = express();

nextApp.prepare().then(() => {
  expressApp.use(APIRouter());
  expressApp.all('*', nextHandler);
  expressApp.listen(port, () => {
    console.log(`App listening on port ${port}`);
  });
});

相关问题