在Heroku上使用NodeJS进行主机服务器端渲染

时间:2019-02-23 19:21:26

标签: node.js reactjs express heroku

我已经使用create-react-app引导了我的React应用。然后,我使用this setup添加了服务器端渲染支持。以下是我的package.json中的脚本。

"start": "react-scripts start",
"build": "react-scripts build",
"dev": "yarn run build && yarn run serve",
"test": "react-scripts test",
"eject": "react-scripts eject",
"heroku-postbuild": "yarn run build && yarn run serve",
"serve": "node ./server/index.js"

服务器是

app.use(express.Router().get("/", loader));
app.use(express.static(path.resolve(__dirname, "../build")));
app.use(loader);
Loadable.preloadAll().then(() => {
  app.listen(PORT, console.log(`App listening on port ${PORT}!`));
});

app.on("error", error => {
  if (error.syscall !== "listen") {
    throw error;
  }

  const bind = typeof PORT === "string" ? "Pipe " + PORT : "Port " + PORT;

  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    default:
      throw error;
  }
});

我试图在Heroku上运行此应用程序。但这不起作用。这是我的日志文件。

$ yarn run build && yarn run serve
   $ react-scripts build
   Creating an optimized production build...
   Compiled with warnings.

   ./src/app/routes/articles/index.js
     Line 2:  'bindActionCreators' is defined but never used  no-unused-vars
     Line 3:  'connect' is defined but never used             no-unused-vars

   ./src/app/blog/Components/SocialMenu.js
     Line 3:  'Message' is defined but never used  no-unused-vars

   ./src/app/routes/index.js
     Line 3:   'AuthenticatedRoute' is defined but never used    no-unused-vars
     Line 4:   'UnauthenticatedRoute' is defined but never used  no-unused-vars
     Line 9:   'Homepage' is assigned a value but never used     no-unused-vars
     Line 21:  'Dashboard' is assigned a value but never used    no-unused-vars
     Line 27:  'Login' is assigned a value but never used        no-unused-vars
     Line 33:  'Logout' is assigned a value but never used       no-unused-vars
     Line 39:  'Profile' is assigned a value but never used      no-unused-vars

   ./src/app/blog/Utils/utilFunctions.js
     Line 8:   Expected '!==' and instead saw '!='  eqeqeq
     Line 8:   Expected '!==' and instead saw '!='  eqeqeq
     Line 11:  Expected '!==' and instead saw '!='  eqeqeq
     Line 14:  Expected '!==' and instead saw '!='  eqeqeq
     Line 17:  Expected '!==' and instead saw '!='  eqeqeq
     Line 20:  Expected '!==' and instead saw '!='  eqeqeq
     Line 23:  Expected '!==' and instead saw '!='  eqeqeq
     Line 26:  Expected '!==' and instead saw '!='  eqeqeq
     Line 29:  Expected '!==' and instead saw '!='  eqeqeq

   Search for the keywords to learn more about each warning.
   To ignore, add // eslint-disable-next-line to the line before.

   File sizes after gzip:

     102.7 KB (-3.49 KB)   build/static/css/9.c633c3ae.chunk.css
     81.92 KB (-25.44 KB)  build/static/js/9.3d9d2903.chunk.js
     5.11 KB (-1.2 KB)     build/static/js/main.271f7ff0.chunk.js
     3.34 KB (-954 B)      build/static/js/profile.c81abdd1.chunk.js
     3.12 KB (-191 B)      build/static/css/main.73e308c6.chunk.css
     1.37 KB (-378 B)      build/static/js/logout.ee33954a.chunk.js
     1.35 KB (-366 B)      build/static/js/login.fd4d2586.chunk.js
     1.23 KB (-283 B)      build/static/js/runtime~main.64122811.js
     442 B (-108 B)        build/static/js/articles.2025307c.chunk.js
     424 B (-65 B)         build/static/js/dashboard.9d863e2e.chunk.js
     297 B (-53 B)         build/static/js/homepage.72f79bfe.chunk.js
     274 B (-47 B)         build/static/js/about.1a16bdd8.chunk.js

   The project was built assuming it is hosted at https://mushfiqweb.com.
   You can control this with the homepage field in your package.json.

   The build folder is ready to be deployed.
   You may serve it with a static server:

     yarn global add serve
     serve -s build     

   $ node ./server/index.js
   App listening on port 8080!

$ node ./server/index.js App listening on port 8080! -----> Timed out running buildpack Node.js ! Timed out compiling app (31 minutes) ! See https://devcenter.heroku.com/articles/slug-compiler#time-limit

1 个答案:

答案 0 :(得分:0)

Heroku的解决方案:

<div style="clear: left; clear: right">中,需要更改以下内容:

package.json