我用React,Node和Mongodb创建了一个简单的待办事项应用程序。该应用程序在开发服务器上运行时运行良好。但是,当我部署它时,似乎该应用程序无法访问todo API路由。任何帮助将不胜感激,我已经为此奋斗了两天,但无济于事。感谢您的阅读。
package.json:
"name": "server",
"version": "1.0.0",
"description": "",
"main": "index.js",
"engines": {
"node": "14.0.0",
"npm": "6.14.5"
},
"proxy": "http://localhost:5000",
"dependencies": {
"body-parser": "^1.19.0",
"concurrently": "^5.2.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"mongodb": "^3.5.9",
"mongoose": "^5.9.21"
},
"devDependencies": {},
"scripts": {
"server": "nodemon index.js",
"start": "node index.js",
"client": "npm run start --prefix client",
"sass": "npm run sass --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\" ",
"heroku-postbuild": "npm install --prefix client && npm run build --prefix client"
},
"author": "",
"license": "ISC"
}
这是我的前端get呼叫示例:
axios
.get("/get/list")
.then((response) => {
response.data.map((list) => {
this.setState({ lists: [...this.state.lists, list] });
return this.state.lists;
});
})
.catch((err) => console.log(err));
}
我后端的index.js:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const keys = require("./config/keys");
const cors = require("cors");
const app = express();
const List = require("./models/List");
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(express.json());
mongoose.connect(keys.MONGO_URI);
if (process.env.NODE_ENV === "production") {
// Express will serve up production assets
// like our main.js file, or main.css file
app.use(express.static("client/build"));
const path = require("path");
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
require("./routes/listRoutes")(app);
app.get("/test", (req, res) => {
console.log("test proxy works");
res.send("test proxy works");
});
const PORT = process.env.PORT || 5000;
app.listen(process.env.PORT || 5000);
和listRoutes文件:
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const List = require("../models/List");
module.exports = (app) => {
// gets ALL List from database
app.get("/get/list", async (req, res) => {
let lists = await List.find({}, (err) => {
err ? console.log(err) : null;
});
console.log(lists);
res.send(lists);
});
// Create a new List
app.post("/new/list", (req, res) => {
let newList = new List({
title: req.body.title,
lists: [
{
category: "house",
importance: 4,
body: "Fix the fire detector",
notes: "for safety",
},
],
created: new Date().toUTCString(),
});
newList.save((err) => {
err;
});
res.send(newList);
});
// add new list item to it's List
app.post("/new/listItem/:id", (req, res) => {
const { category, body, importance, notes } = req.body;
// creates new list item to be updated in the database
var listUpdate = {
category,
body,
importance,
notes,
};
console.log(listUpdate);
// finds the selected list and updates the lists array with a new list object
List.findOneAndUpdate(
{ _id: req.params.id },
{ $push: { lists: listUpdate } },
function (error, success) {
if (error) {
console.log(error);
} else {
console.log(success);
}
}
);
res.send(listUpdate);
});
// Find and edit a list item inside of a list
app.post("/update/listItem/:id", async (req, res) => {
let id = req.params.id;
const { category, body, importance, notes } = req.body;
const listItemUpdate = {
category,
body,
importance,
notes,
};
// this will require placeholder values of the previous list item values to prevent them from being overwritten with nothing
await List.updateOne(
// finds a database entry that matches both the _id and the subdocument collection id passed to the url
{ "lists._id": id },
// sets the list object using the position identifier ($) to locate the specific
// array index and updates it with the variable containing the req.body
{ $set: { "lists.$": listItemUpdate } },
(err, item) => {
console.log(item);
res.send(item);
}
);
});
};