【发布时间】:2020-06-06 11:19:55
【问题描述】:
所以我构建了一个简单的 mern 应用程序,该应用程序在我的本地环境中运行良好,但是在部署到 Heroku 时,它可以很好地服务于 react 应用程序,但在 API 调用上会出现 404。我似乎无法弄清楚这个问题。我正在使用 Axios 发送请求。我检查了网络请求,它们看起来都不错,但仍然返回 404。在 postman 中测试也返回了相同的错误。
这是服务器代码...知道为什么会失败吗?
const express = require('express');
const path = require('path');
const Axios = require('axios');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
require('dotenv').config();
const PORT = process.env.PORT || 8080;
const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/book';
const app = express();
// Define middleware here
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
// Serve up static assets (usually on heroku)
if (process.env.NODE_ENV === 'production') {
app.use(express.static('client/build'));
}
const { Schema } = mongoose;
const bookSchema = new Schema({
info: Schema.Types.Mixed,
});
const Book = mongoose.model('Book', bookSchema);
app.post('/api/search', (req, res) => {
Axios.get(
`https://www.googleapis.com/books/v1/volumes?q=${req.body.term}`
).then(books => res.json(books.data.items));
});
app.post('/api/save', (req, res) => {
const newBook = new Book({ info: req.body.book });
newBook.save(err => {
if (err) res.json(err);
res.json({ status: true });
});
});
app.post('/api/unsave', (req, res) => {
Book.findByIdAndRemove(req.body.book._id, err => {
if (err) res.json(err);
res.json({ status: true });
});
});
app.get('/api/saved', (req, res) => {
Book.find({}, (err, books) => {
if (err) res.json(err);
res.json(books);
});
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, './client/build/index.html'));
});
mongoose.connect(mongoUri, { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('connected');
});
app.listen(PORT, () => {
console.log(`???? ==> API server now on port ${PORT}!`);
});
这是我的 package.json
{
"name": "google-book",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"start": "if-env NODE_ENV=production && npm run start:prod || npm run start:dev",
"start:prod": "node server.js",
"start:dev": "concurrently \"nodemon --ignore 'client/*'\" \"npm run client\"",
"client": "cd client && npm run start",
"install": "cd client && npm install",
"build": "cd client && npm run build",
"heroku-postbuild": "npm run build"
},
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.19.2",
"body-parser": "^1.19.0",
"concurrently": "^5.1.0",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"mongodb": "^3.5.3",
"mongoose": "^5.9.1"
}
}
我的反应路线以防出现问题
return (
<div className="app">
<Header />
<Router>
<Route exact path='/'>
<Searchbar search={search} setSearch={setSearch} />
{!search.term ? (
<div className="message">
<p>Search for a book or whatever</p>
</div>
) : <SearchList results={search.results} />}
</Route>
<Route path='/saved'>
<h2 className="title">Saved Books</h2>
<SavedList />
</Route>
<Footer />
</Router>
</div>
);
【问题讨论】:
-
heroku 日志中的任何内容?浏览器控制台呢?前端代码是什么样的?您愿意分享网址吗?
-
除了 404 错误之外的任何日志都没有任何用处。这真的很奇怪,因为它在我的本地环境中运行良好(代理)。这是链接googly-book.herokuapp.com 这是请求的样子:请求 URL:googly-book.herokuapp.com/api/search 请求方法:POST 状态代码:404 未找到远程地址:18.211.160.51:80 推荐人策略:no-referrer-when-downgrade
-
Heroku 中的日志是怎么说的?
-
2020-02-22T03:09:27.294399+00:00 heroku[router]: at=info method=POST path="/api/search" host=googly-book.herokuapp.com request_id =aa832fd7-c70e-4317-b1ed-5a8128841ee3 fwd="76.247.3.91" dyno=web.1 connect=0ms service=1ms status=404 bytes=417 protocol=http
-
是的,这真的很奇怪......我没有什么特别的 - 一切看起来都应该正常工作。
标签: javascript node.js reactjs express heroku