0
我做下面的教程:不能運行的Node.js應用程序,因爲:「在失敗views目錄查找視圖‘錯誤’」
https://www.raymondcamden.com/2017/02/08/using-social-login-with-passport-and-node/
然後,我一直在創造這個項目(我「M使用Windows 10
):
> mkdir stackoverflow-question
> cd stackoverflow-question
> git clone https://github.com/napolev/stackoverflow-question.git .
> npm install
> gulp start-mongod
> Ctrl+C
> gulp run
但是,要在瀏覽器中打開網站時:http://localhost:4000/
我得到的弗洛翼錯誤:
Error: Failed to lookup view "error" in views directory "D:\wamp64\www\ionic2\stackoverflow-question\views"
at EventEmitter.render (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\application.js:580:17)
at ServerResponse.render (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\response.js:971:7)
at D:\wamp64\www\ionic2\stackoverflow-question\app.js:153:9
at Layer.handle_error (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\router\layer.js:71:5)
at trim_prefix (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\router\index.js:315:13)
at D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\router\index.js:284:7
at Function.process_params (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\router\index.js:335:12)
at IncomingMessage.next (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\router\index.js:275:10)
at done (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\response.js:966:25)
at EventEmitter.render (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\application.js:582:14)
at ServerResponse.render (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\response.js:971:7)
at D:\wamp64\www\ionic2\stackoverflow-question\app.js:127:9
at Layer.handle [as handle_request] (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\router\layer.js:95:5)
at next (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\router\route.js:137:13)
at isLoggedIn (D:\wamp64\www\ionic2\stackoverflow-question\app.js:123:5)
at Layer.handle [as handle_request] (D:\wamp64\www\ionic2\stackoverflow-question\node_modules\express\lib\router\layer.js:95:5)
這是該文件的源代碼:app.js
:
var express = require('express');
var exphbs = require('express-handlebars');
var user = require('./models/user');
var credentials = require('./credentials.json');
var passport = require('passport');
var TwitterStrategy = require('passport-twitter').Strategy;
var FacebookStrategy = require('passport-facebook').Strategy;
var CookieParser = require('cookie-parser');
var ExpressSession = require('express-session');
var mongoose = require('mongoose');
passport.use(new FacebookStrategy({
clientID: credentials.facebook.app_id,
clientSecret: credentials.facebook.app_secret,
callbackURL: credentials.facebook.callback,
profileFields:['id','displayName','emails']
}, function(accessToken, refreshToken, profile, done) {
console.log(profile);
var me = new user({
email:profile.emails[0].value,
name:profile.displayName
});
/* save if new */
user.findOne({email:me.email}, function(err, u) {
if(!u) {
me.save(function(err, me) {
if(err) return done(err);
done(null,me);
});
} else {
console.log(u);
done(null, u);
}
});
}
));
passport.use(new TwitterStrategy({
consumerKey: credentials.twitter.consumer_key,
consumerSecret: credentials.twitter.consumer_secret,
callbackURL: credentials.twitter.callback,
includeEmail:true
},
function(token, tokenSecret, profile, done) {
var me = new user({
email:profile.emails[0].value,
name:profile.displayName
});
/* save if new */
user.findOne({email:me.email}, function(err, u) {
if(!u) {
me.save(function(err, me) {
if(err) return done(err);
done(null,me);
});
} else {
console.log(u);
done(null, u);
}
});
}
));
passport.serializeUser(function(user, done) {
console.log(user);
done(null, user._id);
});
passport.deserializeUser(function(id, done) {
user.findById(id, function(err, user) {
done(err, user);
});
});
var app = express();
app.use(CookieParser(credentials.cookieSecret));
app.use(ExpressSession({
resave:false,
saveUninitialized:false,
secret:credentials.cookieSecret
}));
app.use(passport.initialize());
app.use(passport.session());
app.engine('handlebars', exphbs({
defaultLayout:'main'
}));
app.set('view engine', 'handlebars');
var opts = {
server: {
socketOptions: { keepAlive: 1 }
}
};
switch(app.get('env')) {
case 'development':
mongoose.connect(credentials.mongo.development.connectionString, opts);
break;
case 'production':
mongoose.connect(credentials.mongo.production.connectionString, opts);
break;
default:
throw new error('Unknown execution environment: ', app.get('env'));
}
app.set('port', process.env.PORT || 4000);
app.use(express.static(__dirname + '/public'));
function isLoggedIn(req, res, next) {
req.loggedIn = !!req.user;
next();
}
app.get('/', isLoggedIn, function(req, res) {
res.render('index', {
title:'Welcome to Fool',
loggedIn:req.loggedIn
});
});
app.get('/auth/facebook', passport.authenticate('facebook', {scope:"email"}));
app.get('/auth/facebook/callback', passport.authenticate('facebook',
{ successRedirect: '/', failureRedirect: '/login' }));
app.get('/auth/twitter', passport.authenticate('twitter', {scope:['include_email=true']}));
app.get('/auth/twitter/callback', passport.authenticate('twitter',
{ successRedirect: '/', failureRedirect: '/login' }));
app.get('/login', isLoggedIn, function(req, res) {
if(req.loggedIn) res.redirect('/');
console.log(req.loggedIn);
res.render('login', {
title:'Login/Registration'
});
});
// 500 error handler (middleware)
app.use(function(err, req, res, next){
console.error(err.stack);
res.status(500);
res.render('error');
});
app.listen(app.get('port'), function() {
console.log('Express running on http://localhost:' + app.get('port'));
});
如何使這個代碼(從上面的教程)任何想法的作品?
現在我需要解決上述錯誤。
你在哪裏設置視圖目錄,如'app.set('views',somePath);'。 – jfriend00
我沒有在我的帖子中引用的教程中看到類似的句子。你有什麼合適的例子嗎?我只是想讓這個教程有效。 – davidesp