Heroku反应部署
Heroku react deploy
所以构建运行了,但它显然没有使用正确的 package.json 文件,因为它获得的唯一依赖项是 npm。当我到达我部署的网站时,我从 Heroku 收到默认的应用程序错误。这些是构建日志:
Logs
我的文件布局如下所示:
Visual Studio
这是 server.js 文件:
const express = require('express')
const mongoose = require('mongoose')
const PORT = process.env.PORT || 3000
const jwtAuth = require("./middleware/jwtAuth")
require("dotenv").config()
const cors = require('cors');
const authRoutes = require('./routes/auth')
const orderRoutes = require('./routes/order')
const axios = require('axios')
const corsOptions = {
origin: 'http://localhost:3000',
}
const app = express()
//this will convert the request into json, since node doesn't accept json by default
app.use(express.json());
//this will enable cors on localhost
app.use(cors(corsOptions));
app.use('/api/auth', authRoutes)
app.use('/portfolio', jwtAuth)
app.use('/api/order', orderRoutes)
app.get('/secret', jwtAuth, (req, res) => {
res.send('Secret Hello World!')
})
//fetch testing
app.use('/stock', jwtAuth, function(req, res, next) {
const apikey = "M7DSRJECMBCEEWGF";
const ticker = ['MSFT']
let completed = 0;
const results = [];
console.log(ticker);
for (let i = 0; i < ticker.length; i++) {
const oneTicker = ticker[i];
axios.get(
`https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${ticker}&apikey=${apikey}`
)
.then((response) => {
completed += 1;
results.push(response.data);
if (completed === ticker.length) {
//All ticker have finished their response
console.log("completed");
res.send({
success: true,
message: "Ticker info",
results,
});
}
console.log(ticker);
})
.catch((e) => {
console.error(e);
});
}
})
app.get('/auth', jwtAuth, (req, res) => {
res.status(200).send("Welcome home, cowboy")
})
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/secret', jwtAuth, (req, res) => {
res.send('Secret Hello World!')
})
app.get('*', (req, res) => {
res.send('This route does not exist')
})
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen(PORT, () => console.log(`Server started on PORT ${PORT}`))
})
.catch((err) => {
console.log(err)
process.exit(1)
})
嘿嘿,我真的很想了解 heroku 上的部署是如何工作的! :)
您在 devDependencies
中定义了 mongoose
,do 在 slug 创建阶段安装,但在实际应用程序之前被删除已部署,即 documented here.
By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies.
After running the installation and build steps Heroku will strip out the packages declared under devDependencies before deploying the application.
根据这些信息,您可以选择将 mongoose
移动到 dependencies
- 这可能是您想要的。
不过,如果您想将其保留在 devDependencies
之下,您可以通过更新配置来获得 Heroku keep them in the built slug。 虽然我不推荐这个,除非你有充分的理由将它作为开发依赖项保留。
heroku config:set NPM_CONFIG_PRODUCTION=false YARN_PRODUCTION=false
所以构建运行了,但它显然没有使用正确的 package.json 文件,因为它获得的唯一依赖项是 npm。当我到达我部署的网站时,我从 Heroku 收到默认的应用程序错误。这些是构建日志:
Logs
我的文件布局如下所示: Visual Studio
这是 server.js 文件:
const express = require('express')
const mongoose = require('mongoose')
const PORT = process.env.PORT || 3000
const jwtAuth = require("./middleware/jwtAuth")
require("dotenv").config()
const cors = require('cors');
const authRoutes = require('./routes/auth')
const orderRoutes = require('./routes/order')
const axios = require('axios')
const corsOptions = {
origin: 'http://localhost:3000',
}
const app = express()
//this will convert the request into json, since node doesn't accept json by default
app.use(express.json());
//this will enable cors on localhost
app.use(cors(corsOptions));
app.use('/api/auth', authRoutes)
app.use('/portfolio', jwtAuth)
app.use('/api/order', orderRoutes)
app.get('/secret', jwtAuth, (req, res) => {
res.send('Secret Hello World!')
})
//fetch testing
app.use('/stock', jwtAuth, function(req, res, next) {
const apikey = "M7DSRJECMBCEEWGF";
const ticker = ['MSFT']
let completed = 0;
const results = [];
console.log(ticker);
for (let i = 0; i < ticker.length; i++) {
const oneTicker = ticker[i];
axios.get(
`https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=${ticker}&apikey=${apikey}`
)
.then((response) => {
completed += 1;
results.push(response.data);
if (completed === ticker.length) {
//All ticker have finished their response
console.log("completed");
res.send({
success: true,
message: "Ticker info",
results,
});
}
console.log(ticker);
})
.catch((e) => {
console.error(e);
});
}
})
app.get('/auth', jwtAuth, (req, res) => {
res.status(200).send("Welcome home, cowboy")
})
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.get('/secret', jwtAuth, (req, res) => {
res.send('Secret Hello World!')
})
app.get('*', (req, res) => {
res.send('This route does not exist')
})
mongoose
.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
app.listen(PORT, () => console.log(`Server started on PORT ${PORT}`))
})
.catch((err) => {
console.log(err)
process.exit(1)
})
嘿嘿,我真的很想了解 heroku 上的部署是如何工作的! :)
您在 devDependencies
中定义了 mongoose
,do 在 slug 创建阶段安装,但在实际应用程序之前被删除已部署,即 documented here.
By default, Heroku will install all dependencies listed in package.json under dependencies and devDependencies.
After running the installation and build steps Heroku will strip out the packages declared under devDependencies before deploying the application.
根据这些信息,您可以选择将 mongoose
移动到 dependencies
- 这可能是您想要的。
不过,如果您想将其保留在 devDependencies
之下,您可以通过更新配置来获得 Heroku keep them in the built slug。 虽然我不推荐这个,除非你有充分的理由将它作为开发依赖项保留。
heroku config:set NPM_CONFIG_PRODUCTION=false YARN_PRODUCTION=false