简体   繁体   English

node.js在包含的js文件中表达渲染

[英]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 这是myscript.js

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

Can I make express rendering engine (in this case ejs ) render inside myscript.js ? 我可以在myscript.js内部制作快速渲染引擎(在这种情况下为ejs )吗?

I don't believe express will touch your static files. 我不认为Express会触及您的静态文件。 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 . 使用正则表达式路由,您可以使用任何以.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. 不过,将静态javascript提供给使用Express提供的JSON数据的浏览器可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM