4

I'm starting to use NodeJs. I'm currently using Express to render a view, and I want to setup some conditionals.

My development environment uses NodeMon and Reload to automatically refresh the browser each time I make modifications to the source code. I've setup my dependencies as follows:

{
 "scripts": {
 "start": "nodemon app.js"
 },
 "dependencies": {
 "express": "3.4.8",
 "ejs-locals": "1.0.2",
 "ejs": "*"
 },
 "devDependencies": {
 "nodemon": "1.0.14",
 "grunt": "*",
 "grunt-contrib-less": "~0.9.0",
 "grunt-contrib-cssmin": "~0.7.0",
 "reload": "~0.1.0"
 }
}

This ensures that my production environment doesn't load up anything that's not really necessary for the app to run.

Reload uses a javascript file located in /reload/reload.js in order to refresh the browser. This file is added on my main layout.ejs file so it runs on all views.

I'm trying to do something like this so that it doesn't get rendered on the production server:

<%if (process.env.NODE_ENV === 'development') { %>
 <script src="/reload/reload.js"></script>
<% } %>
  • This doesn't work, what is the correct way to check for the current environment from the view?
  • Is it OK to do so? does this violate some pattern?
  • Should I just pass the current environment setting to the views directly from the controller?
asked Feb 12, 2014 at 5:35

1 Answer 1

5

Try:

<%if (settings.env === 'development') { %>
 <script src="/reload/reload.js"></script>
<% } %>

Separately, ejs-locals isn't maintained and so shouldn't be used.

answered Feb 12, 2014 at 21:11
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I was using app.locals in order to pass the variable. This is much better.
Where did you set the settings?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.