I'm trying to connect to endpoint, I have "POST http://localhost:3000/api/post 404 (Not Found)"
connecting middleware postRoutes to the ./api/index.js:
app.use('/post', postRoutes)
export default {
path: '/api',
handler: app
}
postRouter file ./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
vue page:
const formData = {
title: this.form.title,
text: this.form.text
}
try {
await this.$store.dispatch('post/create', formData)
} catch (e) {}
so I make a post request to the store ./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
}
}
}
CodePudding user response:
if you write your full path in axios it will work. Also you can create field in state for base url and use it in actions.
return await this.$axios.$post('http://localhost:3000/api/post', formData)
CodePudding user response:
I solved it this way by editing router.post before that middleware was observed on /api/post/api/post:
router.post('/', ctr.create)
also my request looks like that:
return await this.$axios.$post('/api/post', formData)
