我在使用 nodeJS 和 express 时收到错误 404
I am getting an error 404 using nodeJS and express
我在名为 review 的项目中遇到路线问题。我的其他路线没有问题,所以我不确定我哪里出错了。我的前端和邮递员不断收到错误 404。我相信一切都链接到正确的信息。我走路线
http://localhost:8080/api/review/addReview 得到 404 错误
这是我的server.js
const express = require("express");
const cors = require("cors");
const dbConfig = require("./app/config/db.config");
const app = express();
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
const db = require("./app/models");
// const Role = db.role;
app.use('/uploads', express.static('uploads'));
db.mongoose
.connect(`mongodb+srv://password@cluster0.gmvao.mongodb.net/shop?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => {
console.log("Successfully connect to MongoDB.");
// initial();
})
.catch(err => {
console.error("Connection error", err);
process.exit();
});
// routes
// require(".app/routes/favourite.routes")(app);
require("./app/routes/auth.routes")(app);
require("./app/routes/user.routes")(app);
app.use('/api/admin', require('./app/routes/admin.routes'));
app.use('/api/review', require('./app/routes/review.routes'));
// set port, listen for requests
const PORT = 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
我的路线文件
const express = require('express');
const router = express.Router();
const {authJwt} = require("../middlewares");
const Review = require("../models/review.model")
router.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
router.post("/addReview", [authJwt.verifyToken], (req, res) => {
const review = new Review(req.body)
review.save((err, review) => {
if(err) return res.json({success:false, err})
Review.find({'_id': review._id})
.populate('author')
.exec((err, result) => {
if(err) return res.json({success: false, err})
return res.status(200).json({success: true, result})
})
})
})
module.exports = router ;
我的评论模型文件
const mongoose = require('mongoose');
const Review = mongoose.model(
"Review",
new mongoose.Schema({
prodID: String,
productTitle: String,
reviewId: String,
content: String,
author: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
]
})
);
module.exports = Review;
一切正常。您的 api/review/addReview 路由方法是 POST,所以请检查您是从 Postman 发出 post 请求。
我在名为 review 的项目中遇到路线问题。我的其他路线没有问题,所以我不确定我哪里出错了。我的前端和邮递员不断收到错误 404。我相信一切都链接到正确的信息。我走路线
http://localhost:8080/api/review/addReview 得到 404 错误
这是我的server.js
const express = require("express");
const cors = require("cors");
const dbConfig = require("./app/config/db.config");
const app = express();
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(express.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(express.urlencoded({ extended: true }));
const db = require("./app/models");
// const Role = db.role;
app.use('/uploads', express.static('uploads'));
db.mongoose
.connect(`mongodb+srv://password@cluster0.gmvao.mongodb.net/shop?retryWrites=true&w=majority`, {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false
})
.then(() => {
console.log("Successfully connect to MongoDB.");
// initial();
})
.catch(err => {
console.error("Connection error", err);
process.exit();
});
// routes
// require(".app/routes/favourite.routes")(app);
require("./app/routes/auth.routes")(app);
require("./app/routes/user.routes")(app);
app.use('/api/admin', require('./app/routes/admin.routes'));
app.use('/api/review', require('./app/routes/review.routes'));
// set port, listen for requests
const PORT = 8080;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
我的路线文件
const express = require('express');
const router = express.Router();
const {authJwt} = require("../middlewares");
const Review = require("../models/review.model")
router.use(function(req, res, next) {
res.header(
"Access-Control-Allow-Headers",
"x-access-token, Origin, Content-Type, Accept"
);
next();
});
router.post("/addReview", [authJwt.verifyToken], (req, res) => {
const review = new Review(req.body)
review.save((err, review) => {
if(err) return res.json({success:false, err})
Review.find({'_id': review._id})
.populate('author')
.exec((err, result) => {
if(err) return res.json({success: false, err})
return res.status(200).json({success: true, result})
})
})
})
module.exports = router ;
我的评论模型文件
const mongoose = require('mongoose');
const Review = mongoose.model(
"Review",
new mongoose.Schema({
prodID: String,
productTitle: String,
reviewId: String,
content: String,
author: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "User",
},
]
})
);
module.exports = Review;
一切正常。您的 api/review/addReview 路由方法是 POST,所以请检查您是从 Postman 发出 post 请求。