【发布时间】:2021-06-07 20:04:08
【问题描述】:
我有这个前端代码:
export const CreatePage = () => {
const auth = useContext(AuthContext)
const {request} = useHttp()
const [content, setContent] = useState('')
const [title, setTitle] = useState('')
const [lead, setLead] = useState('')
useEffect(() => {
window.M.updateTextFields()
},[])
const postHandler = async () => {
try {
const data = await request('/api/post/generate', 'POST', {title: title, lead: lead, content: content}, {
Authorization: `Bearer ${auth.token}`
})
console.log(data)
} catch (e) {}
}
还有这个后端代码:
router.post('/generate', auth, async (req, res) => {
try {
const baseURL = config.get('baseURL')
const {title, lead, content} = req.body
// if (!title || !lead || !content) {
// return res.status(422).json({error: 'Please, input ALL fields'})
// }
const Post = new Post({
title, lead, content, owner: req.body.user.userId // req.user.userId
})
await Post.save()
res.status(201).json({Post})
} catch (e) {
res.status(500).json({message: 'Something went wrong'})
}})
我已经尝试了很多东西,但我仍然收到此错误。我知道这是一个服务器端错误,但这就是我能够弄清楚的全部。
附:如果对代码有任何疑问,我会在稍后补充。
UPD:顺便问一下,这可能是问题的原因吗?控制台日志:
[1] Proxy error: Could not proxy request /api/post/generate from localhost:3000 to http://localhost:5000.
【问题讨论】:
-
通常表示后端服务器关闭。
-
不,我使用 npm run dev (concorrently)。这个带有登录系统的应用程序。并且登录系统正常工作。
-
你能给我们看看
package.json文件吗? -
我猜你对
"proxy": "http://localhost:5000"感兴趣
标签: node.js reactjs mongodb express mern