I need to use a process variable inside my ejs template in order to call an endpoint, but from this context I cannot reach the process nodejs variable.
How can I achieve this?
<a class="imgLink" href="#" onclick="get_user_info()">
<div style="border: 2px solid gray;padding: 8px">Info</div>
</a>
<script>
function get_user_info() {
$.get(`/users/${process.env.userId}`, function(data) {
// Do something
})
}
</script>
asked Aug 13, 2018 at 16:37
Julian Torregrosa
8611 gold badge10 silver badges22 bronze badges
-
1Just a remark for best practice, your Environment variables shoul have capitalized name, in your case: 'uSER-ID' .Ale Gu– Ale Gu2020年07月24日 06:24:04 +00:00Commented Jul 24, 2020 at 6:24
1 Answer 1
<a class="imgLink" href="#" onclick="get_user_info()">
<div style="border: 2px solid gray;padding: 8px">Info</div>
</a>
<script>
function get_user_info() {
var userId= '<%= process.env.userId %>';
$.get(`/users/${userId}`, function(data) {
// Do something
})
}
</script>
get the userId using <%= process.env.userId%>
answered Aug 13, 2018 at 18:53
Edwin Babu
7198 silver badges15 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Julian Torregrosa
It works but <%= process.env.userId %> should have quotes
var userId= '<%= process.env.userId %>';Patrick Roberts
What about just
$.get('/users/<%= process.env.userId %>', function (data) { ... })?lang-js