[英]production calling api from localhost
I have deployed my application (created with npm run build
) to heroku. 我已经将我的应用程序(使用
npm run build
)部署到了heroku。 However, the api calls done on heroku production are from my localhost. 但是,在heroku生产上完成的api调用来自我的本地主机。 How do I change this?
我该如何改变? Could anyone please advice?
有人可以请教吗?
api_axios.js api_axios.js
const axios = require('axios')
export default () => {
return axios.create({
baseURL: 'https://myapp.herokuapp.com/api/' || 'http://localhost:1991/api/'
})
}
server.js server.js
const express = require('express')
const bodyParser = require('body-parser')
const cors = require('cors')
const port = process.env.PORT || 1991
// express app
const app = express()
// middleware
app.use(cors())
// routes
const metrics = require('./routes/api/metrics')
app.use('/api/metrics', metrics)
// handle production
if(process.env.NODE_ENV === 'production'){
// static folder
app.use(express.static(__dirname + '/public/'))
// handle spa
app.get(/.*/, (req, res) => res.sendFile(__dirname + '/public/index.html'))
}
// run server
const server = app.listen(port, ()=>{
console.log(`server running on port ${port}`)
})
Just like you check process.env.NODE_ENV
in your server, you should also check environment when you compile your JavaScript. 就像检查服务器中的
process.env.NODE_ENV
一样,在编译JavaScript时也应检查环境。
You can use environment variables (via process.env
as above), configuration files (such as require('./config.json')
, or any other method you like. At the end of the day though, you shouldn't hardcode your API URL. 您可以使用环境变量(通过如上所述的
process.env
),配置文件(例如require('./config.json')
或您喜欢的任何其他方法。但是,到了一天结束时,您不应该硬编码您的API URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.