Next.js 应用程序部署为 ssr 应用程序,但有一些托管服务提供商限制
Next.js app deployment as ssr app with some hosting provider restrictions
我有一个支持托管节点应用程序的共享托管服务提供商。它有一个限制,即应用程序入口文件必须称为 app.js
并且必须在文件夹 /usr/home/username/domains/domain/public_nodeapp
中。该应用程序会在首次从网络访问域时自动启动(可能通过类似 node app.js
的方式)。
是否可以在这样的提供商上托管 next.js
应用程序作为服务器端呈现的应用程序(而不是 next export
生成的静态 HTML 站点)?
在运行 next build
制作应用程序的生产版本后,生产版本没有 app.js
文件,应该由 next start
启动。我不确定是否以及如何对其进行调整(可能是一些文件移动或重命名)以符合上述限制。
你可以在需要的地方放一个app.js
文件,然后调用cli触发next start
时会调用的函数。
这意味着该文件应该需要 https://github.com/zeit/next.js/blob/canary/packages/next/cli/next-start.ts 并调用 nextStart
。
// app.js
const startServer = require('next/dist/cli/next-start');
startServer();
是的,您可以使用自定义服务器来做到这一点,例如。 express.js
我正在为 Azure 做同样的事情,唯一的区别是我需要一个名为 server.js
的文件
示例:
// app.js
const express = require('express');
const next = require('next');
const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.all('*', (req, res) => handle(req, res));
server.listen(port, (err) => {
if (err) throw err;
/* eslint no-console: "off" */
console.log(`> Ready on http://localhost:${port}`);
});
});
希望对您有所帮助。
我有一个支持托管节点应用程序的共享托管服务提供商。它有一个限制,即应用程序入口文件必须称为 app.js
并且必须在文件夹 /usr/home/username/domains/domain/public_nodeapp
中。该应用程序会在首次从网络访问域时自动启动(可能通过类似 node app.js
的方式)。
是否可以在这样的提供商上托管 next.js
应用程序作为服务器端呈现的应用程序(而不是 next export
生成的静态 HTML 站点)?
在运行 next build
制作应用程序的生产版本后,生产版本没有 app.js
文件,应该由 next start
启动。我不确定是否以及如何对其进行调整(可能是一些文件移动或重命名)以符合上述限制。
你可以在需要的地方放一个app.js
文件,然后调用cli触发next start
时会调用的函数。
这意味着该文件应该需要 https://github.com/zeit/next.js/blob/canary/packages/next/cli/next-start.ts 并调用 nextStart
。
// app.js
const startServer = require('next/dist/cli/next-start');
startServer();
是的,您可以使用自定义服务器来做到这一点,例如。 express.js
我正在为 Azure 做同样的事情,唯一的区别是我需要一个名为 server.js
示例:
// app.js
const express = require('express');
const next = require('next');
const port = process.env.PORT || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.all('*', (req, res) => handle(req, res));
server.listen(port, (err) => {
if (err) throw err;
/* eslint no-console: "off" */
console.log(`> Ready on http://localhost:${port}`);
});
});
希望对您有所帮助。