我无法从玉内部调用我的 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 ),以及尝试将脚本直接放在根目录和“视图”目录中。
应用程序.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
这似乎是相关的,但没有解决问题:静态 javascript not rendering in jam (with express/node.js)