【发布时间】: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"
}
【问题讨论】:
-
请分享您的 index.js 文件
-
添加,index.html 也是 create-react-app 的样板模板
-
然后你以错误的方式部署它
-
请分享您的项目结构和节点 index.js 文件,而不是反应 index.js。
-
更新了,我猜节点 index.js 文件是指我的 server.js 文件?
标签: javascript node.js reactjs heroku