http-party / http-server

a simple zero-configuration command-line http server
MIT License
13.65k stars 1.5k forks source link

Feature : Auto replace certain strings in file / Inject env variables #414

Closed Telokis closed 6 years ago

Telokis commented 6 years ago

I am currently working on a react front-end application that I'd like to embed in a docker container using http-server.

This app has some configuration that needs to be done and I'd like to do that after the app is minified/compressed.

One solution I thought would be nice is to alter the files that http-server is sending. Using some pattern we could inject env variables into the app by simply saying something like so:

<p>
NODE_ENV is %MY_ENV_VAR%
</p>

This is an example of input that http-serve would intercept (if the variable MY_ENV_VAR is available in process.env) and inject in place of %MY_ENV_VAR%.

SidneyNemzer commented 6 years ago

This sounds like something you'd want to do at build-time. Since you're using React, I assume you're also using Webpack, so you could use the DefinePlugin.

Telokis commented 6 years ago

This app has some configuration that needs to be done and I'd like to do that after the app is minified/compressed.

I know I could do that at build time but I don't want to rebuild the whole app for that simple thing. The app itself will almost never change but this config part will change a lot between my clients.

BigBlueHat commented 6 years ago

http-server is focused on static site hosting, so dynamically replacing text in any file is out of scope.

I agree with @SidneyNemzer that doing this at build time is best--if you want to stay satic. Otherwise, moving to Server Side Rendering (SSR), is a better solution.