如何使用JADE从express.js返回带有(java)脚本的/ response

时间:2013-05-10 14:16:06

标签: express pug

我想在我的express.js应用程序上使用javascript脚本响应ajax调用,这些脚本将在客户端进行评估。如何使用JADE作为动态构建这些脚本的模板来处理它?<​​/ p>

很多。 马库斯

1 个答案:

答案 0 :(得分:0)

在你的应用中:

app.get('/demo.js', function(req, res) {
  res.set('Content-Type', 'text/javascript');
  res.render('script', { value : Math.random() });
});

在您的模板(script.jade)中:

|var someVar = #{value};

测试:

$ http get localhost:3012/demo.js
...
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 33
Content-Type: text/javascript
Date: Fri, 10 May 2013 14:30:54 GMT
X-Powered-By: Express

var someVar = 0.9364965052809566;