Home > front end >  What to change 'localhost' with?
What to change 'localhost' with?

Time:03-01

I just finished my Node app, which is working fine, but I'm not experienced about deployment.

exports.update_ibare = (req, res) =>{
axios.get('http://localhost:3000/api/ibare', { params : { id : req.query.id }})
    .then(function(response){
        res.render("update_ibare", { ibareler : response.data})
    })
    .catch(err =>{
        res.send(err);
    })}

What am I going to change localhost with, before I deploy this? I'm going to deploy to gcloud.

CodePudding user response:

The best method here would be to pull the URL from an environment variable from the host system as environment variables are normally easy to setup and allow your code to be as portable as possible, but this is only possible when compiling the code using a module bundler such as Webpack. Some example code is below.

exports.update_ibare = (req, res) => {
  axios
    .get(`${process.env_API_URL}/api/ibare`, { params : { id : req.query.id }})
    .then(function(response){
        res.render("update_ibare", { ibareler : response.data})
    })
    .catch(err =>{
        res.send(err);
    })
}

In the event that you aren't using a bundler or compiler, I'd suggest that you start too, so you can replace strings like this with environment variables to make your code portable.

  • Related