How to get the base url variable on a deployed Heroku node app

heroku environment variables
heroku procfile node
heroku react environment variables
heroku config vars node js
heroku build environment variables
install heroku cli npm
get url of heroku app
node getting started heroku

I tried the following but it doesn't work:

console.log(process.env.BASE_URL);   #output: undefined

Say my heroku app's host name is: https://RyanCameron-app.herokuapp.com

How should I go about getting that base_url on heroku?

I am looking for a solution to avoid to respectively uncomment/comment out the following lines:

const url = 'https://RyanCameron-app.herokuapp.com/projects/eovendo' //production 
//const url = 'http://localhost:3000'                                //development

It has become quite annoying ..

Backend

This checks if you are in production or development mode and assigns the according url depending on the mode.

const production  = 'https://examplePage.com';
const development = 'http://localhost:3000/';
const url = (process.env.NODE_ENV ? production : development);

Explanation: process.env.NODE_ENV will resolve to undefined if you are running on localhost production mode. And return production if you have deployed the app production mode.

Note: just because you have deployed the app doesn't necessarily mean you have changed the NODE_ENV


Frontend

Make use of the javascript window to extract the url

const url = window.location.origin;

Configuration and Config Vars, A step-by-step guide for deploying your first Node app and mastering the basics of Heroku. How to get the base url variable on a deployed Heroku node app. Ask Question Asked 1 year, 4 months ago. Set a config variable, HEROKU_APP_NAME or HOST

You can use Base URL in JavaScript

// This article: // https://stackoverflow.com/questions/21246818/how-to-get-the-base-url-in-javascript

var base_url = window.location.origin; // "http://stackoverflow.com"

var host = window.location.host; // stackoverflow.com

var pathArray = window.location.pathname.split( '/' ); // ["", "questions", "21246818", "how-to-get-the-base-url-in-javascript"]

Getting Started on Heroku with Node.js, URL ENV variable on Heroku and access them through a mini webserver from your Angular app!. Tagged with angular, heroku, env, node. Now visit the app at the URL generated by its app name. As a handy shortcut, you can open the website as follows: $ heroku open View logs. Heroku treats logs as streams of time-ordered events aggregated from the output streams of all your app and Heroku components, providing a single channel for all of the events.

Set a config variable, HEROKU_APP_NAME or HOST - something obvious. You can access that in the backend but then you can also write it out into the DOM and have your front end access it from there.

Backend URL ENV Variable on Angular using Heroku Config Vars , @feathersjs/configuration is a wrapper for node-config which allows to docs you can organize "hierarchical configurations for your app deployments". you have to set the NODE_CONFIG_DIR environment variable in app.js going to use environment variables (e.g. set by Heroku) and use public/dist to  Now, go back to the Deploy tab, and click Deploy Branch at the bottom. Heroku will take the code and host it. Open the Activity tab and there you can see the progress: And that’s it! Open the settings tab and scroll down to the Domains and certificates section. Here, you can see the URL of your app that was just deployed.

Configuration, An app I'm building has a Create-React-App (CRA) client side and Node.js server side hosted on two separate heroku deployments. Located in the REACT_APP_SERVER_URL); var baseUrl = ''; if (process.env. NODE_ENV during the build do you know of anyway to access this variable post-build? That isn't really ideal so let's rename that to heroku-node. $ heroku apps:rename heroku-node --app calm-reaches-6216. That command doesn't work since Heroku will inform us that the app name is taken. Let's add a number to our new name and move on. $ heroku apps:rename heroku-node-8080 --app calm-reaches-6216

CRA sees but does not consume Environmental variables · Issue , Let's change the attribute baseUrl in the src/services/notes.js like so: Now frontend's GET request to http://localhost:3001/api/notes does not work for some reason: Keep in mind, that same origin policy and CORS are not specific to React or Node. Heroku configures application port based on the environment variable. Heroku supports the latest Stable version of Node.js as well as all active LTS (Long-Term-Support) versions. Currently supported versions are 10.x, 12.x, and 13.x. Other available runtimes. Since Heroku is based on a standard Ubuntu Linux stack, you can run most Node versions (>= 0.10.0) on the platform. However, the testing and support focus

Fullstack part3, Have you seen in webpack site (https://webpack.js.org/vote/) there is a page where path for img on React.js · How to deploy node that uses Webpack to heroku [hash].bundle.js Your application bundled [ size: 149 KB for new Angular CLI your html template, to specify the base url of all relative urls in your document. To determine how to start your app, Heroku first looks for a Procfile. If no Procfile exists for a Node.js app, we will attempt to start a default web process via the start script in your package.json. The command in a web process type must bind to the port number specified in the PORT environment variable. If it does not, the dyno will not start.

Comments
  • "ReferenceError: window is not defined". Unfortunately the solution does not work backend - I know I didn't specify whether it should work back- or frontend but I was actually looking for a backend way. Anyway upvoted for an answer that works for frontend.
  • The config variable sounds like I would have to uncomment/comment whenever I deploy the app. And to write it out in the frontend just to send it to the backend sounds like a pain - basically Marat Badykov's solution.