静态javascript不渲染在玉(用express / node.js) [英] static javascript not rendering in jade (with express/node.js)

查看:100
本文介绍了静态javascript不渲染在玉(用express / node.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望你很好。

我突然无法在玉模板中渲染任何外部javascript!为了达到最底层的目的,我把它从最低限度上解决了:

I'm suddenly unable to render any external javascript in jade templates! To get to the bottom of things, I stripped it down to the bare minimum :

节点0.6.11,Express 2.5.8,玉0.20.3

Node 0.6.11, Express 2.5.8, jade 0.20.3

app.js

var express = require('express')
, routes = require('./routes');

var app = module.exports = express.createServer();

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure('production', function(){
  app.use(express.errorHandler());
});

app.get('/', function(req, res){
  res.render('index', { title: 'Express' });
});

app.listen(3000);

layout.jade

layout.jade

!!!
html
  head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
    script(type='text/javascript', src='/javascripts/script.js')
  body!= body

script.js

script.js

alert('hello!');

在我看来,当我运行服务器并加载 http:// localhost:3000 / ,我应该直接得到一个你好!消息,但它直接运行到正常页面。
更重要的是,如果我手动输入

It seems to me that when I run the server and load http://localhost:3000/, I should straightaway get a 'hello!' message, but it just runs straight to the normal page. What's more, if I manually type

script
  alert('hello!')

进入layout.jade模板我得到的消息就像我应该的。它只是不加载静态脚本。我确实检查'script.js'是在'/ public / javascripts /',就像它应该是。
任何建议将是非常欢迎!!!

into the layout.jade template I get the message just as I should. It is just not loading the static script. And I have certainly checked that 'script.js' is in '/public/javascripts/' just as it should be. Any advice would be very welcome!!!

提前感谢

推荐答案

您需要从控制器传递您的脚本:

you need to pass your script from the controller like that:

app.get('/', function(req, res){
  res.render('index', { title: 'Express', scripts: ['javascripts/script.js']});
});

然后在layout.jade的头上:

and then in your head in layout.jade:

- each s in scripts
    script(src=s)

这篇关于静态javascript不渲染在玉(用express / node.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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