只有 GET '/' 路由在我的 MERN 项目中有效,使用 POSTMAN 进行测试得到 404 响应
Only GET '/' route is working in my MERN project, testing with POSTMAN getting 404 responses
我的 app.js 看起来像这样:
const express = require('express');
const connectDB = require('./config/db');
var cors = require('cors');
const fruits = require('./routes/api/fruits');
const app = express();
connectDB();
app.use(cors({origin: true, credentails: true}));
app.use(express.json({extended: false}));
app.get('/', (req, res) => res.send('Hello World'));
app.use('/api/fruits', fruits);
const port = process.env.PORT || 8082;
app.listen(port, () => console.log(`Server running on port ${port}`));
我的 fruit.js 在 routes/api/fruits.js
const express = require('express');
const router = express.Router();
const Fruit = require('../../models/Fruit');
router.get('/test', (req, res) => {
res.send('fruit route testing!');
console.log('Route found');
});
router.get('/', (req, res) => {
Fruit.find()
.then(fruits => res.json(fruits))
.catch(err => res.status(404).json({nofruitsfound : 'No Fruit Found'}));
});
router.get('/:id', (req, res) => {
Fruit.findById(req.params.id)
.then(fruit => res.json(fruit))
.catch(err => res.status(404).json({nofruitsfound : 'No Fruit Found'}));
});
router.post('/', (req, res) => {
Fruit.create(req.body)
.then(fruit => res.json({ mgs: 'Fruit added sucessfully'}))
.catch(err => res.status(400).json({ error: 'Unable to add this fruit'}));
});
router.put(':/id', (req, res) => {
Fruit.findByIdAndUpdate(req.params.id, req.body)
.then(fruit => res.json({ mgs: 'Updated successfully'}))
.catch(err => res.status(400).json({ error: 'Unable to update the Database'}));
});
router.delete('/:id', (req, res) => {
Fruit.findByIdAndDelete(req.params.id, req.body)
.then(fruit => res.json({ mgs: 'Fruit deleted sucessfully'}))
.catch(err => res.status(404).json({ error: 'Unable to find fruit by Id and delete'}));
});
module.exports = router;
http://localhost:8082/ 获取“Hello World”
http://localhost:8082/api/fruit/test 获取:
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /test</pre>
</body>
</html>
我也试过 http://localhost:8082/api/fruits/test 正在工作但是 POST for http://localhost:8082/api/fruits/ 正在收到 {"error ":"无法添加该水果"}
postman request for POST http://localhost:8082/api/fruits/
您没有定义 /test
或 /api/test
。 /api/fruit/test
将是您与具有 :id
参数的端点最接近的。
请注意,尽管您有一个 .
字符,但您的 express 应用正在使用您的 /api/fruit
路由。它说 /api/fruit.
基于这一行 app.use('/api/fruits.', fruits);
,您已经在 fruits.js
文件中为 endpoints/routes 定义了基本路径。这意味着为了能够访问路由,您必须将基本路径作为前缀添加到 url,如下所示:
http://localhost:8082/api/fruits/test
- 将获得 /test
路线
http://localhost:8082/api/fruits
- 将获得 fruits.js
中定义的所有水果
http://localhost:8082/api/fruits
- 与 POST 请求一起使用,您可以创建一个新水果。
你的基本路径加上主机是这样的:http://localhost:8082/api/fruits
看来您是 Express.js 的新手,所以我要添加:
app.use(express.json({extended: false}));
- 此行告诉 express 仅处理 JSON 中的请求正文。因此,在 Postman 或您使用的任何其他 HTTP 客户端中,确保 POST 请求的正文是 JSON.
我的 app.js 看起来像这样:
const express = require('express');
const connectDB = require('./config/db');
var cors = require('cors');
const fruits = require('./routes/api/fruits');
const app = express();
connectDB();
app.use(cors({origin: true, credentails: true}));
app.use(express.json({extended: false}));
app.get('/', (req, res) => res.send('Hello World'));
app.use('/api/fruits', fruits);
const port = process.env.PORT || 8082;
app.listen(port, () => console.log(`Server running on port ${port}`));
我的 fruit.js 在 routes/api/fruits.js
const express = require('express');
const router = express.Router();
const Fruit = require('../../models/Fruit');
router.get('/test', (req, res) => {
res.send('fruit route testing!');
console.log('Route found');
});
router.get('/', (req, res) => {
Fruit.find()
.then(fruits => res.json(fruits))
.catch(err => res.status(404).json({nofruitsfound : 'No Fruit Found'}));
});
router.get('/:id', (req, res) => {
Fruit.findById(req.params.id)
.then(fruit => res.json(fruit))
.catch(err => res.status(404).json({nofruitsfound : 'No Fruit Found'}));
});
router.post('/', (req, res) => {
Fruit.create(req.body)
.then(fruit => res.json({ mgs: 'Fruit added sucessfully'}))
.catch(err => res.status(400).json({ error: 'Unable to add this fruit'}));
});
router.put(':/id', (req, res) => {
Fruit.findByIdAndUpdate(req.params.id, req.body)
.then(fruit => res.json({ mgs: 'Updated successfully'}))
.catch(err => res.status(400).json({ error: 'Unable to update the Database'}));
});
router.delete('/:id', (req, res) => {
Fruit.findByIdAndDelete(req.params.id, req.body)
.then(fruit => res.json({ mgs: 'Fruit deleted sucessfully'}))
.catch(err => res.status(404).json({ error: 'Unable to find fruit by Id and delete'}));
});
module.exports = router;
http://localhost:8082/ 获取“Hello World”
http://localhost:8082/api/fruit/test 获取:
<html lang="en">
<head>
<meta charset="utf-8">
<title>Error</title>
</head>
<body>
<pre>Cannot GET /test</pre>
</body>
</html>
我也试过 http://localhost:8082/api/fruits/test 正在工作但是 POST for http://localhost:8082/api/fruits/ 正在收到 {"error ":"无法添加该水果"}
postman request for POST http://localhost:8082/api/fruits/
您没有定义 /test
或 /api/test
。 /api/fruit/test
将是您与具有 :id
参数的端点最接近的。
请注意,尽管您有一个 .
字符,但您的 express 应用正在使用您的 /api/fruit
路由。它说 /api/fruit.
基于这一行 app.use('/api/fruits.', fruits);
,您已经在 fruits.js
文件中为 endpoints/routes 定义了基本路径。这意味着为了能够访问路由,您必须将基本路径作为前缀添加到 url,如下所示:
http://localhost:8082/api/fruits/test
- 将获得 /test
路线
http://localhost:8082/api/fruits
- 将获得 fruits.js
http://localhost:8082/api/fruits
- 与 POST 请求一起使用,您可以创建一个新水果。
你的基本路径加上主机是这样的:http://localhost:8082/api/fruits
看来您是 Express.js 的新手,所以我要添加:
app.use(express.json({extended: false}));
- 此行告诉 express 仅处理 JSON 中的请求正文。因此,在 Postman 或您使用的任何其他 HTTP 客户端中,确保 POST 请求的正文是 JSON.