2017-02-24 56 views
1

我試圖使用node.js,express.js和mongodb構建購物車。我正在構建註冊功能。我的問題是,當我嘗試訪問本地主機:3000 /用戶/個人資料鏈接我收到此錯誤信息:無法在視圖目錄中查找'/ user/profile'

Failed to lookup view "/user/profile" in views directory 
    "/Users/vynguyen/shopping-cart/views 

    Error: Failed to lookup view "/user/profile" in views directory "/Users/vynguyen/shopping-cart/views" 
     at EventEmitter.render (/Users/vynguyen/shopping-cart/node_modules/express/lib/application.js:579:17) 
     at ServerResponse.render (/Users/vynguyen/shopping-cart/node_modules/express/lib/response.js:960:7) 
     at /Users/vynguyen/shopping-cart/routes/index.js:34:6 
     at Layer.handle [as handle_request] (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/layer.js:95:5) 
     at next (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/route.js:131:13) 
     at Route.dispatch (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/route.js:112:3) 
     at Layer.handle [as handle_request] (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/layer.js:95:5) 
     at /Users/vynguyen/shopping-cart/node_modules/express/lib/router/index.js:277:22 
     at Function.process_params (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/index.js:330:12) 
     at next (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/index.js:271:10) 
     at csrf (/Users/vynguyen/shopping-cart/node_modules/csurf/index.js:117:5) 
     at Layer.handle [as handle_request] (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/layer.js:95:5) 
     at trim_prefix (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/index.js:312:13) 
     at /Users/vynguyen/shopping-cart/node_modules/express/lib/router/index.js:280:7 
     at Function.process_params (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/index.js:330:12) 
     at next (/Users/vynguyen/shopping-cart/node_modules/express/lib/router/index.js:271:10 

的profile.hbs文件位於正確的文件夾,但對於一些因爲服務器不會把它拉起來。

這裏的整個文件結構: enter image description here

這是我的index.js文件:

var express = require('express'); 
var router = express.Router(); 
var csrf = require('csurf'); 
var passport = require('passport'); 

var Product = require('../models/product'); 

var csrfProtection = csrf(); 
router.use(csrfProtection); 

/* GET home page. */ 
router.get('/', function(req, res, next) { 
    Product.find(function(err, docs) { 
     var productChunks = []; 
     var chunkSize = 3; 
     for (var i = 0; i < docs.length; i += chunkSize) { 
      productChunks.push(docs.slice(i, i + chunkSize)); 
     } 
     res.render('shop/index', { title: 'Shopping Cart', products: productChunks }); 
    }); 
}); 

router.get('/user/signup', function(req, res, next) { 
    res.render('user/signup', {csrfToken: req.csrfToken()}); 
}); 

router.post('/user/signup', passport.authenticate('local.signup', { 
    successRedirect: '/user/profile', 
    failureRedirect: '/user/signup', 
    failureFlash: true 
})); 

router.get('/user/profile', function(req, res, next){ 
    res.render('/user/profile'); 
}); 

module.exports = router; 

這裏是我的app.js:

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 expressHbs = require('express-handlebars'); 
var mongoose = require('mongoose'); 
var session = require('express-session'); 
var passport = require('passport'); 
var flash = require('connect-flash'); 

var routes = require('./routes/index'); 
var userRoutes = require('./routes/user'); 

var app = express(); 

mongoose.connect('localhost:27017/shopping'); 
require('./config/passport'); 

// view engine setup 
app.set('views', __dirname+'/views'); 
app.engine('.hbs', expressHbs({defaultLayout: 'layout', extname: '.hbs'})); 
app.set('view engine', '.hbs'); 

// 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: 'mysupersecret', resave: false, saveUninitialized: false})); 

app.use(express.static(path.join(__dirname, 'public'))); 

app.use(flash()); 
app.use(passport.initialize()); 
app.use(passport.session()); 

app.use('/user', userRoutes); 
app.use('/', routes); 

// 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'); 
}); 

module.exports = app; 
+0

嘗試'用戶/ profile' –

+0

想這和有一個 '404未找到' 錯誤 – vnguyen

+0

怎麼樣」 ./user/(不以 '/' 開始)個人資料「? – 2017-02-24 05:23:09

回答

1

當視圖文件路徑解析,帶前導斜槓的視圖路徑將解析爲根目錄,因此出現錯誤。您需要解析相對於views目錄的視圖文件。在res.render方法中使用user/profile./user/profile將正確解析視圖文件。

正確的代碼: res.render('user/profile')res.render('./user/profile')

相關問題