[英]Can't access express route?
I am working on a mern project and I have this tiny?我正在做一个 mern 项目,我有这个小? error: When I try to post on a specific route, express returns 404 and can't find the route I wanted.
错误:当我尝试在特定路线上发帖时,快递返回 404 并且找不到我想要的路线。 Everything works fine when I try to post to the index route '/' .
当我尝试发布到索引路由'/'时,一切正常。 Here is a snippet:
这是一个片段:
in /controllers/imageController在/控制器/图像控制器
ImageController图像控制器
exports.image_upload_post = (req,res) => {
console.log('init file posting')
}
in /controllers/accidentController在 /controllers/accidentController
AccidentController (works fine)事故控制器(工作正常)
exports.accident_detail_post = (req,res) => {
console.log('init form data posting')
}
Here are the routes:以下是路线:
routes/imageUpload route:路线/图片上传路线:
const express = require('express')
const router = express.Router()
const image_upload_controller = require('../controllers/ImageUploadController')
router.post('/upload', image_upload_controller.image_upload_post)
module.exports = router
/routes/index route: (the working one) /routes/index 路由:(工作中的)
const express = require('express')
const router = express.Router()
const accident_detail_controller = require('../controllers/AccidentController')
router.post('/', accident_detail_controller.accident_detail_post)
module.exports = router
And finally, server.js:最后,server.js:
const express = require('express')
const cors = require('cors')
const mongoose = require('mongoose')
const app = express()
const indexRouter = require('./routes/index')
const imageUploadRouter = require('./routes/imageUpload')
app.use(cors())
app.use(express.json())
app.use('/', indexRouter)
app.use('/upload', imageUploadRouter)
And my question is, why when I try to do something like this:我的问题是,为什么当我尝试做这样的事情时:
const submitData = async (e) => {
e.preventDefault()
await axios.post('http://localhost:3001/upload', 'test')
}
in the client React app, it returns 404 error?在客户端 React 应用程序中,它返回 404 错误? If I try to post to http://localhost:3001/ I get the controller's log 'init file posting'?
如果我尝试发布到http://localhost:3001/我得到控制器的日志“初始化文件发布”?
You have你有
app.use('/upload', imageUploadRouter)
and in the imageUploadRouter file, you have在 imageUploadRouter 文件中,你有
router.post('/upload', image_upload_controller.image_upload_post)
that means your full path will be这意味着您的完整路径将是
/upload/upload
and URL will be和 URL 将是
http://localhost:3001/upload/upload
Solution : replace the /upload by / in the imageUploadRouter
解决方案:将 imageUploadRouter 中的 /upload 替换为 /
router.post('/', image_upload_controller.image_upload_post)
In this case, the URL will be在这种情况下,URL 将是
http://localhost:3001/upload
The problem is that essentially your route is not http://localhost:3001/upload
but instead http://localhost:3001/upload/upload
.问题是,基本上你的路线不是
http://localhost:3001/upload
而是http://localhost:3001/upload/upload
。 You are defining it once in 'routes/imageUpload rout' and again in 'index.js'.您在 'routes/imageUpload rout' 中定义它一次,然后在 'index.js' 中再次定义它。 An easy fix for this would be to write
app.use('/api', imageUploadRouter)
in the index file.一个简单的解决方法是在索引文件中编写
app.use('/api', imageUploadRouter)
。 Then you can make your requests to http://localhost:3001/api/upload
然后您可以向
http://localhost:3001/api/upload
提出请求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.