node.js在包含的js文件中表达渲染 [英] node.js express rendering inside included js files

查看:94
本文介绍了node.js在包含的js文件中表达渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说一个简单的观点

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

这是 myscript.js

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

我可以制作快速渲染引擎(在这种情况下,是 ejs )呈现在 myscript.js 吗?

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

推荐答案

I不要相信express会碰到您的静态文件。您可以将其设为从路线中渲染并投放的视图,例如:

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'); 
});

使用正则表达式路由,您可以使用任何以 .js 结尾的内容。 (在任何人否决之前,请注意我说的是可以,而不是应该。)

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

您可能会更好不过,向使用使用Express提供的JSON数据的浏览器提供了静态javascript。

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

这篇关于node.js在包含的js文件中表达渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆