问题描述
Axios发布请求在我的本地服务器上运行正常,但是在Google Cloud上部署项目后却抛出404 not found错误。
在本地主机中,所有请求都发送到以http://localhost:9000/api/...
开头的URL,并且可以完美地处理所有发布请求。
但是,在将项目部署到Google云端之后,它会抛出404 error
。
如果URL包含/api/...
并且完整的URL
看起来像这样的https://myurl.org.nz/api/...
,我已经配置Nginx将传入请求重定向到服务器。下面是我的Nginx配置。
代码:
server {
server_name myurl.org.nz www.myurl.org.nz;
location / {
root /home/myproject/build/;
try_files $uri /index.html;
}
location /api/ {
proxy_pass http://localhost:9000/;
}
}
我的处理传入请求的服务器端代码
base.routes.js
router.route("/").post(async (req,res) => {
await axios
.get("https://maps.googleapis.com/maps/api/place/textsearch/json",{
params: {
query: req.body.query.search,key: process.env.GOOGLE_PLACE_API_KEY,},})
// ........ rest of the code.....
})
server.js
const route = require("./Routes/base.routes");
app.use("/api",route);
从客户端发送到服务器的Axios请求。 REACT_APP_PROD_URL=https://www.myurl.org.nz/api/
axios.post(`${process.env.REACT_APP_PROD_URL}`,data)
.then((res) => {
setData(res.data)
})
我不确定我哪里错了。感谢您的帮助,如果您需要更多信息,请告诉我。
谢谢。
解决方法
这是一个超级超级基本应用程序,支持多条路线和您的代码
julia> using ImageFiltering
julia> mapwindow(collect,x,0:1,border=Inner())
5-element OffsetArray(::Array{Array{Int64,1},1:5) with eltype Array{Int64,1} with indices 1:5:
[1,2]
[2,3]
[3,4]
[4,5]
[5,6]
// ./index.js
const express = require('express')
const app = express()
const server = require('http').Server(app)
app.use('/',require('./routes'))
server.listen('3000',() => {
console.log('running on port 3000')
})
// ./routes/index.js
const { Router } = require('express')
const router = Router()
// responds to GET http://localhost:3000
router.get('/',(req,res) => {
res.json({
root: {
api: 'basic api',version: 1
}
})
})
router.use('/api',require('./api'))
module.exports = router
和一些ngninx代码
// ./routes/api.js
const axios = require('axios')
const { Router } = require('express')
const router = Router()
// responds to POST http://localhost:3000/api
router.post('/',async (req,res) => {
try {
const response = await axios
.get("https://maps.googleapis.com/maps/api/place/textsearch/json",{
params: {
query: req.body.query.search,key: process.env.GOOGLE_PLACE_API_KEY,},})
console.log(response)
// ........ rest of the code.....
} catch (err) {
res.status(500).json({
error: err
})
}
})
module.exports = router
,
我通过从proxy_pass localhost:9000中删除forward /来解决此问题。