我正在尝试使用NodeJS学习Express,并希望使用纯HTML呈现我的视图。我基于Express API文档和几个Stack问题一起攻击了一个网络服务器,特别是Andrew Homeyer在this问题中回答的问题
你可以让jade包含一个纯HTML页面:
在views / index.jade中
在views / plain.html中包含plain.html
...而且app.js仍然只能渲染玉石:
res.render(指数)
我的目录结构如下所示
Project
*web.js
Public
img
js
lib
gallerific
*jquery.opacityrollover.js
*jquery.gallerific.js
angular
theme
views
partials
*index.html
*index.jade
我的服务器看起来像这样。
var express = require('express'),
jade = require('jade');
var app = module.exports = express();
app.configure(function(){
app.set('views', __dirname + '/public/views');
app.use("/public/lib", express.static(__dirname + "/public/lib"));
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.set('view engine', 'jade')
app.use(express.bodyParser());
});
app.get('/', function(req, res) {
res.render('index');
});
app.get('/partials/:name', function(req, res){
var name = req.params.name;
res.render('/public/partials/' + name);
});
app.get('/public/data/:name', function(req, res){
var name = req.params.name;
res.json('/public/data/' + name)
});
app.listen(3000, function(){
console.log("Express app listening on port %d in %s mode", this.address().port, app.settings.env);
});
我所看到的是某些文件无法从其他所有内容加载的目录中加载。例如,我的Gallery页面无法从我的lib / gallerific目录加载jquery.gallerific.js javascript文件,同时它加载了jquery.opacityrollover.js。我浏览过Chrome开发者工具并查看以下内容
我让这个网站使用Angular Bootstrap网络服务器,所以它似乎不是客户端代码的javascript错误。有谁知道我可能会做什么会导致这个问题?
答案 0 :(得分:0)
我明白了。事实证明,我必须解决我忘记的路径,以便Express可以正确地渲染它们。并不是Gallerific javascript库没有加载,它在我的图库图像的未定义的图像源上抛出错误(我从JSON文件中提取它们)。
一旦我为图像和数据文件放入适当的路径,一切都开始工作了。感谢所有为我提供建议的人。这真的帮助我解决了这个问题。