【发布时间】:2021-03-10 03:18:55
【问题描述】:
我正在使用 Axios 在我的后端查询一个端点。当我尝试这样做时,我得到一个 404 not found 。如果我从控制台复制/粘贴它在错误中给出的 uri 并尝试直接在浏览器中访问它,它连接正常并且不会给我一个错误(而是给我一个预期的空对象)。
下面是我的 Axios 代码
axios.post("/api/myEndpoint", { id: this.userID })
.then((response) => {
this.property = response.data.property;
})
.catch((errors) => {
console.log(errors);
router.push("/");
});
下面是我后端的路由定义
const myEndpointRoute = require('../api/myEndpoint.js')();
exprApp.use('/api/myEndpoint', myEndpointRoute);
作为参考,URI 是“http://localhost:3000/api/myEndpoint”。我可以在浏览器中完全访问这个 uri,但 Axios 会返回 404,如上所述。正是出于这个原因,我确信这是前端的一个问题,但是我已经以与我拥有的许多其他请求相同的方式设置了这个 Axios 请求,并且它们都可以正常工作。
编辑:这是后端的其余部分
myEndpoint.js
module.exports = function() {
const express = require('express'), router = express.Router();
const authMiddleware = require('../loaders/authMiddleware.js')();
router.get('/', authMiddleware, async function(req, res) {
const id = req.body.id;
const property = await require('../services/myEndpointService.js')
(id).catch((e) => { console.log(e) });
res.send({ property: property });
});
return router;
};
myEndpointService.js
module.exports = async function(id) {
const results = await require('../models/getMyEndpointProperty')(id);
return results;
};
getMyEndpointProperty
module.exports = async function(id) {
const pool = require('../loaders/pool.js')();
const res = await pool.query(`SELECT * FROM myTable WHERE id = ${id};`);
return res.rows;
};
【问题讨论】:
-
你能显示 myEndpoint.js 吗?
-
我添加了一些额外的后端信息
标签: javascript node.js express vue.js axios