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?
1 Answer 1
Try:
<%if (settings.env === 'development') { %>
<script src="/reload/reload.js"></script>
<% } %>
Separately, ejs-locals isn't maintained and so shouldn't be used.