Michael
Michael

Reputation: 22947

node.js express rendering inside included js files

Lets say I have a simple view

<html>
  <head>
    <title>something</title>
  </head>
  <body>
  <%= param %>
  </body>
  <script type="text/javascript" src="myscript.js"></script>
</html>

And here's myscript.js

$(function() {
  var p = <%= param %>
}

Can I make express rendering engine (in this case ejs) render inside myscript.js ?

Upvotes: 0

Views: 6740

Answers (1)

giaour
giaour

Reputation: 4061

I don't believe express will touch your static files. You could make this a view that gets rendered and served from a route, as in:

app.get('/js/myscript.js', function(req, res) { 
    res.render('myscript'); 
});

With regex routes, you could do this with anything ending in .js. (Before anyone downvotes, note that I said could, not should.)

You probably would be better off with static javascript being served to the browser that uses JSON data served from Express, though.

Upvotes: 4

Related Questions