我是NodeJS的新手,想要在书上复制该程序。 查看引擎是玉,我正在使用Bootstrap。我认为静态路径是正确的。
app.use(express.static(__dirname+'/public'));
但是当我安慰它时,它显示了这个,
C:\Users\Dell\Desktop\express\microblog/public
问题只显示当我尝试访问broswer中的页面时,而不是在我激活服务器后立即访问该页面,错误代码为500,如下所示
Cannot read property 'lazyrouter' of undefined
TypeError: Cannot read property 'lazyrouter' of undefined
at route (C:\Users\Dell\Desktop\express\microblog\node_modules\express\lib\application.js:254:7)
at Layer.handle [as handle_request] (C:\Users\Dell\Desktop\express\microblog\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Dell\Desktop\express\microblog\node_modules\express\lib\router\index.js:312:13)
at C:\Users\Dell\Desktop\express\microblog\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Users\Dell\Desktop\express\microblog\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\Dell\Desktop\express\microblog\node_modules\express\lib\router\index.js:271:10)
at C:\Users\Dell\Desktop\express\microblog\node_modules\express-session\index.js:489:7
at C:\Users\Dell\Desktop\express\microblog\node_modules\connect-mongo\lib\connect-mongo.js:306:11
at handleCallback (C:\Users\Dell\Desktop\express\microblog\node_modules\mongodb\lib\utils.js:96:12)
at C:\Users\Dell\Desktop\express\microblog\node_modules\mongodb\lib\collection.js:1335:5
但实际上正确渲染了layout.jade,只有块内容区域显示错误。
下面的app.js
var settings=require('./settings');
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session=require('express-session');
var MongoStore=require('connect-mongo')(session);
var User=require('./models/user');
var crypto=require('crypto');
var index = require('./routes/index');
var users = require('./routes/users');
var post = require('./routes/post');
var reg = require('./routes/reg');
var login = require('./routes/login');
var logout = require('./routes/logout');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: settings.cookieSecret,
resave: false,
saveUninitialized: true,
store: new MongoStore({
db:settings.db
})
}));
app.use(app.route);
app.use(express.static(path.join(__dirname,'/public')));
console.log(__dirname+'/public');
app.use('/', index);
app.use('/users', users);
app.use('/post', post);
app.use('/reg', reg);
app.use('/login', login);
app.use('/logout', logout);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
app.use(function (req,res,next) {
res.locals.user=req.session.user;
var err=req.flash('error');
var success=req.flash('success');
res.locals.err=err.length?err:null;
res.locals.success=success.length?success:null;
next();
})
module.exports = app;
` 下面是layout.jade
的链接部分link(type='text/css',rel='stylesheet', href='/stylesheets/bootstrap.css')
link(type='text/css',rel='stylesheet', href='/stylesheets/bootstrap-responsive.css')
script(src="/javascripts/jquery.js")
script(src="/javascripts/bootstrap.js")
答案 0 :(得分:0)
问题出在路径上。在Windows上,路径使用反斜杠而不是正斜杠构造。
尝试更改此代码
app.use(express.static(__dirname+'/public'));
到这个
app.use(express.static(path.join(__dirname, 'public')));
使用path.join(__dirname, 'public')
将为您处理斜杠或反斜杠。
这是对你的更多参考。 https://gist.github.com/domenic/2790533
希望它有所帮助。
答案 1 :(得分:0)
如以下代码“ / sample”一样,指向示例文件夹的整个路径。(在app.js中)
app.use('/sample',express.static(__dirname + '/public/sample'));
然后,您可以按如下所示链接文件(在查看文件中)
<script src="/sample/sample.js"></script>