1

Say I have an express app:

const app = express();

app.use('/sites', express.static(path.join(__dirname, '/sites')));

And in folder /sites there is index.html.

The html file contains a form that posts to a server address. How can I adapt the URL to which the form posts depending on an environment variable?

For example, is it possible to use a placeholder in index.html that will be replaced by the NodeJS environment variable before sending it as response to the client?

3
  • 1
    Use EJS? or you want it to be without templating engine? Commented Oct 18, 2019 at 23:46
  • That's exactly what I was looking for! Commented Oct 18, 2019 at 23:51
  • @iRohitBhatia and how would I reference the env variable in the <%= exampleVar %> tag? Commented Oct 18, 2019 at 23:55

2 Answers 2

2

You can use express view templating as a solution, check this link express.js

Sign up to request clarification or add additional context in comments.

2 Comments

And something more lightweight? Without templating engine?
unfortunately you can't
1

In continuation to my comment, If you don't mind using templating engine, EJS might be a good alternative

I made a project couple of years back ,when I was learning programming so don't judge the code but that might help you

Anyway to answer your question from comment,

const exampleVar = process.env.exampleVar

app.get("/", function (req, res) {
 res.render("index.ejs", {exampleVar}) 
}

Should probably work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.