我正在嘗試連接到端點,我有“POST http://localhost:3000/api/post 404 (Not Found)”
將中間件 postRoutes 連接到 ./api/index.js:
app.use('/post', postRoutes)
export default {
path: '/api',
handler: app
}
postRouter 檔案 ./api/routes/post.routes.js:
const { Router } = require('express')
const ctr = require('../controllers/post.controller')
const router = Router()
router.post('/api/post', ctr.create)
module.exports = router
頁面:
const formData = {
title: this.form.title,
text: this.form.text
}
try {
await this.$store.dispatch('post/create', formData)
} catch (e) {}
所以我向商店./store/post.js 發出發布請求:
export const actions = {
async create({ commit }, formData) {
try {
return await this.$axios.$post('/api/post', formData)
} catch (e) {
commit('setError', e, { root: true })
throw e
}
}
}
uj5u.com熱心網友回復:
如果您在 axios 中撰寫完整路徑,它將起作用。您也可以在狀態中為基本 url 創建欄位并在操作中使用它。
return await this.$axios.$post('http://localhost:3000/api/post', formData)
uj5u.com熱心網友回復:
router.post在觀察到中間件之前,我通過編輯以這種方式解決了這個問題/api/post/api/post:
router.post('/', ctr.create)
我的請求也是這樣的:
return await this.$axios.$post('/api/post', formData)
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/411371.html
標籤:
