Javascript无法呈现在玉中

时间:2012-12-07 00:37:32

标签: javascript node.js pug

我无法从jade中调用我的script.js文件,但如果插入内联,javascript呈现完全正常。这是我的第一次尝试,所以js文件只是一个警告:

alert("javascript works");

我倾向于认为我不正当地调用剧本。目前layout.jade看起来像:

doctype 5
html
  head
    title= "LeafPress - Hot off the Presses"
    link(rel='stylesheet', href='/stylesheets/style.css')
  block scripts
    script(type='text/javascript', src='/javascripts/script.js')
  body
    block content

我尝试了一些脚本路径的排列(../public/javascripts/script.js; /public/javascripts/script.js; public / javascripts / script.js; javascripts / script.js; script.js),以及尝试将脚本直接放在root和'views'目录中。

app.js:

/**
 * Module dependencies.
 */

var express = require('express')
  , routes = require('./routes/index')
  , user = require('./routes/user')
  , http = require('http')
  , path = require('path');

var app = express();

app.configure(function(){
  app.set('port', process.env.PORT || 3000);
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.favicon());
  app.use(express.logger('dev'));
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser('your secret here'));
  app.use(express.session());
  app.use(app.router);
  app.use(require('stylus').middleware(__dirname + '/public'));
  app.use(express.static(path.join(__dirname, 'public')));
});

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

app.get('/', routes.index);
app.get('/permalink', routes.permalink);
app.get('/users', user.list);

http.createServer(app).listen(app.get('port'), function(){
  console.log("Express server listening on port " + app.get('port'));
});

index.js:

/*
 * GET home page.
 */

exports.index = function(req, res){
  res.render('index', { title: 'Express' });
};

exports.permalink = function(req, res) {
  res.render('permalink', { title: 'Comments page' });
};

我的相关文件层次结构如下:

- public
  - javascripts
    - script.js
- views
  - index.jade
  - layout.jade
- routes
  - index.js

app.js

这看似相关,但没有解决问题:static javascript not rendering in jade (with express/node.js)

1 个答案:

答案 0 :(得分:2)

请尝试完全删除此行app.use(app.router);。手动更改快速中间件堆栈中路由器的位置通常会产生不希望的行为。除非你确实知道你为什么要这样做以及你期望发生什么,否则不要这样做。快速记录器为臭名昭着的cannot GET /javascripts/script.js输出的内容,它将直接指向中间件/路由配置错误。

我猜你的降价语法是你的问题刚被破坏,但确保script.js文件在javascripts目录中,而不是它的兄弟。