bobkilla
bobkilla

Reputation: 121

How to avoid hard coding the server url

I am pretty new to the javascript world. I have an application with Angular2 + Webpack using Typescript and I want to define the url of my server inside a property file in order to be able to change it after the build.

It's like a config.js file accessible after the build and accessible in dev mode too.

First: is it a good idea? Second: why am I feeling like I am the only one with this kind of problem?

If it is a good idea, how do you do this?

Upvotes: 2

Views: 2152

Answers (2)

Aaron
Aaron

Reputation: 24812

If you only have one host, that is all your content is served from the same server or your backend server is accessible through the same domain name and port than your frontend server, you should use relative URLs.

Upvotes: 3

Tim McNamara
Tim McNamara

Reputation: 1398

Yes, absolutely it is a good idea, and no, this is not a problem.

Typically you would define a default config and subsequent ENV specific config that may/not override the default. A common way of doing this is to set an ENV variable that your application can process and choose the ENV specific config.

Have a look at MEAN.io and their seed project, they do elegantly.

Webpack can detect the changes and reload the application. Obviously not something you want to do in PROD

Upvotes: 2

Related Questions