是nextjs应用程序的入口点吗?

时间:2020-01-26 10:35:05

标签: reactjs npm cpanel next.js

将nextjs应用程序部署到托管的c-panel时,会询问该应用程序的入口点,默认值为app.js。在正常的React应用程序中,它完全处于控制之中,但在使用nextjs时,不清楚哪个js文件可用于启动应用程序。

enter image description here

关于选择正确的js文件作为应用程序入口点的任何想法?

编辑:

我的托管服务提供商向我提供了以下代码,以设置一个快速应用程序(使用next的请求处理程序)来处理请求:

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

const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();

const port = 3454;

nextApp.prepare().then(() => {
  const app = express();

  app.get('*', (req, res) => {
    return handle(req, res);
  });

  app.listen(port, err => {
    if (err) throw err;
    console.log(`> Ready on localhost:${port}`);
  });
});

它可以工作,但是速度很慢,因为它可以根据需要将源文件编译为服务器请求。

3 个答案:

答案 0 :(得分:3)

您只需要导出nextjs应用程序,它将与

一起使用

页面-是否有页面

  • index.js
  • example.js

app.js -是否有一个app.js文件

只需添加以下脚本

 "scripts": {
    "build": "next build",
    "export": "next export",
    "serve": "serve out"
  },

您可以先构建您的项目,然后将其导出。然后您就可以检查它的部署方式。

如果使用cPanel,只需将可能名为out的nextjs构建文件夹提取到xyz.com之类的文件夹中。

在构建中将有一个index.html作为您的主文件。

答案 1 :(得分:1)

我很惊讶地看到cpanel具有启动nodejs应用程序的功能。

您需要了解有关app.js的哪些内容:

App.js包含一个Web服务器应用程序(从上面的代码中,您的托管服务提供商建议您使用ExpressJS-最常用的JS Web服务器应用程序)将Web文件提供给浏览器(类似于Apache)。

”它可以工作,但是速度很慢,因为它会按需将源文件编译为 服务器请求。”

您有package.json文件吗?

您知道cpanel运行了什么命令来启动您的应用程序吗?

请检查您的NextJS应用程序是否在开发或生产模式下运行。

答案 2 :(得分:0)

对我来说,最好的解决方案是使用某个进程管理器来引导应用程序。因此,我使用PM2启动了应用程序并使其保持运行。我在c-panel终端中发出了以下命令,现在运行良好:

NODE_ENV=production pm2 start server.js

给出的server.js具有以下代码:

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

const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();

const port = 3454;

nextApp.prepare().then(() => {
  const app = express();

  app.get('*', (req, res) => {
    return handle(req, res);
  });

  app.listen(port, err => {
    if (err) throw err;
    console.log(`> Ready on localhost:${port}`);
  });
});