javascript - express get url 错误

标签 javascript express axios

我正在向 express url“getstatus”发送 axios 请求 - 在我的本地开发中一切正常,但是一旦文件在服务器上,url 路径中仍然有我的本地主机。

this.$axios.get('api/getstatus', {
}).then(function (response) {
})
.catch(function (error) {
});

app.get('/getstatus', async (req, res) => {
  // stuff happening
})

-> 在本地主机上正常 -> 服务器错误:请求 URL:http://localhost:3000/api/getstatus

为什么我的本地开发 url 仍然被使用?应该是http://myserver.com/api/getstatus

最佳答案

看来 axios get 请求应该是 /api/getstatus 而不是 api/getstatus

另一件事是你应该在你的点环境文件中设置一个 API_URL 变量,在开发中将它设置为本地主机,在你的服务器上将它设置为你的服务器 URL。

像这样

this.$axios.get(`${process.env.API_URL}/api/getstatus`, {
}).then(function (response) {
   // Code here
})
catch(function (error) {
   // Error code here
});

关于javascript - express get url 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54675665/

相关文章:

javascript - 是什么阻止 querySelectorAll 在此代码中处理具有相同类的所有元素?

javascript - 正确文件夹中的 Wordpress、Javascript 和 Php : Am I placing authenticate-user. php?

javascript - 页面刷新清除所有数据

node.js - 使用csrf(express + node)时禁止访问

javascript - Axios 不在请求中传递 header

django - 在 Vue 3 中设置全局 Axios header

javascript - Multer 无法将文件上传到我的服务器,已经尝试了两天了,眼袋游戏在这一款中很强

javascript - 忽略子文档 MongoDB 中的某些结果

node.js - 将 Cordova 的文件传输插件与 Express/blob 存储结合使用

javascript - axios.get 请求返回 GET,HEAD 作为响应