node.js express rendering inside included js files

女生的网名这么多〃 提交于 2019-12-11 23:33:51

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/19687149/node-js-express-rendering-inside-included-js-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!