【问题标题】:React/Node app in Heroku, serving index.html/index.js with server runningHeroku 中的 React/Node 应用程序,在服务器运行时提供 index.html/index.js
【发布时间】:2020-09-20 01:04:53
【问题描述】:

我很确定这个问题已本地化为我的启动脚本。在线查看时,人们会发布不同的启动脚本。

有些有 "start": "node index.js" ->(这不会启动我的服务器),在 heroku 日志中给出 H10 错误

其他人有“start”:“nodemon server.js”->(这会运行服务器,服务于 index.html,但不会包含 index.js 文件),没有给出错误但不是期望的行为

我使用了 create-react-app,当我可以同时执行“react-scripts start”和“nodemon server”时,它可以在本地工作,并且我的端口也设置为环境变量。 如果我能提供额外的信息,请告诉我,我们将不胜感激

索引.js

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

服务器.js

require('dotenv').config();

var express = require("express");
var app = express();
const cookieParser = require('cookie-parser');
var withAuth = require('./middleware');
const path = require('path');
var PORT = process.env.PORT || 4090;

const { 
    receivePublicToken,
    getTransactions,
    getBalance,
    putCat,
    getCat,
    logIn,
    isUser
} = require("../src/controllers/controller");

app.use(express.json());
app.use(cookieParser());

// Get the public token and exchange it for an access token
app.post("/auth/public_token", withAuth, receivePublicToken);// Get Transactions
app.get("/transactions", getTransactions);
app.get("/accounts/balance/get", getBalance);
app.post("/users/login", logIn);
app.post("/categories/post", putCat);
app.get("/categories/get", getCat);
app.get("/auth", withAuth, isUser);
app.get("/logout", (req, res) => {
    res.clearCookie('token').sendStatus(200);
})
app.get('*', (req, res) => { 
    res.sendFile(path.join(__dirname, '../public/index.html')) 
});

app.listen(PORT, () => {
    console.log(`Server running on ${PORT}`);
});

在 package.json 中启动脚本

"scripts": {
  "dev": "concurrently \"npm run start\" \"npm run start-client\"",
  "start-client": "react-scripts start"
  "start": "nodemon server/server.js",
  "build": "react-scripts build",
  "test": "react-scripts test",
  "eject": "react-scripts eject",
  "postinstall": "npm run build"
  }

directory structure

【问题讨论】:

  • 请分享您的 index.js 文件
  • 添加,index.html 也是 create-react-app 的样板模板
  • 然后你以错误的方式部署它
  • 请分享您的项目结构和节点 index.js 文件,而不是反应 index.js。
  • 更新了,我猜节点 index.js 文件是指我的 server.js 文件?

标签: javascript node.js reactjs heroku


【解决方案1】:

我认为你的启动脚本一定是node server/server.js

【讨论】:

  • 它在本地工作,但是当我在 heroku 中运行它时,我得到一个空白页。当我将 html 添加到 index.html 时,它会显示出来,但它不会为 index.js 和 html 提供服务。我不确定是否有某种方法可以使 javascript 使用反应应用程序索引文件运行..
  • 这是一个正确的运行方式,唯一的区别是,您不必更改 react dir 中 public 文件夹中 index.html 文件中的任何内容。如果你的 html 中没有 &lt;div id="root"&gt;&lt;/div&gt;,那么 react 根本不会渲染任何东西。
  • 如果你想添加任何html,你将添加到src/App.tsx文件中,而不是在html中
  • index.html 只会在 body 和 head 中包含基本的单个 div,仅此而已,您将在 src 中的 jsx 文件中编写您的 html 和所有内容
  • 如果要在本地运行项目,需要先运行npm run build再运行npm run start
【解决方案2】:

你制作了 Procfile 吗?如果没有,请在主目录中创建一个名为“Procfile”的文件,并将 web: node Server.js 添加到其中。这应该告诉 heroku 在它启动时要做什么。

【讨论】:

  • 我认为没有必要。如果您查看他的 package.json,您会看到他为应用程序提供服务的默认命令是 start。所以不需要添加Procfile
  • 90% 的时间都可以工作,但是 heoku 的一些服务器不会听启动命令(当它发生时真的很烦人)然后会出现内部错误
  • 我试过了,它根本没有改变行为,所以我又回去了,谢谢你的建议
  • 好的,很酷。我假设你有这条线,或者类似的东西,但我唯一能想到的可能会阻止它为 index.js 文件提供服务: app.use('/client', express.static(__dirname + ' /client'));
  • 这是我一直在考虑的其他事情,从技术上讲,我没有“客户端”文件夹,但我有一个公共目录,其中包含 index.html、robot.txt 等,我在我的 src 目录中有一个 index.js,其中还包含我的 app.js、控制器等。
猜你喜欢
  • 1970-01-01
  • 2019-01-22
  • 2014-07-10
  • 1970-01-01
  • 2019-06-18
  • 1970-01-01
  • 2020-01-09
  • 2021-10-21
  • 2016-10-12
相关资源
最近更新 更多