簡體   English   中英

Heroku 中的 React/Node 應用程序,在服務器運行的情況下提供 index.html/index.js

[英]React/Node app in Heroku, serving index.html/index.js with server running

我很確定此問題已本地化為我的啟動腳本。 在網上查找時,人們會發布不同的啟動腳本。

有些有“start”:“node index.js”->(這不會啟動我的服務器),在 heroku 日志中給出 H10 錯誤

其他人有“開始”:“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"
  }

目錄結構

我認為您的啟動腳本必須是node server/server.js

你做了一個Procfile嗎? 如果沒有,請在主目錄中創建一個名為“Procfile”的文件,並將 web: node Server.js 添加到其中。 這應該告訴 heroku 啟動時要做什么。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM