Mean Stack应用程序中的部分页面呈现问题

我通过Pluralsight教程学习Mean Stack。 我在某些时候渲染部分2天。

你可以参考下面的图片获取我的文件夹结构。

请参阅图片

server.js

/** * Created by nyaconcepts on 4/10/2014. */ var express=require('express'); var env=process.env.NODE_ENV=process.env.NODE_ENV||'development'; var app=express(); var config=require('./server/config/config')[env]; require('./server/config/express')(app,config); require('./server/config/mongoose')(config); require('./server/config/routes')(app); app.listen(config.port); console.log('Listening on port'+config.port+'---'); 

express.js

 var express=require('express'), bodyParser = require('body-parser'), morgan = require('morgan'), stylus=require('stylus'); module.exports=function(app,config) { function compile(str,path){ return stylus(str).set('filename',path); } app.set('views',config.rootPath+'server/views'); app.set('view engine','jade'); app.use(morgan('dev')); app.use(bodyParser()); app.use(stylus.middleware({ src:config.rootPath+'/public', compile:compile } )); app.use(express.static(config.rootPath+'/public')); } 

routes.js

 module.exports=function(app){ app.get('/partials/*',function(req,res){ console.log("req ="+req +"and res="+res); res.render('../../public/app/'+ req.params); }); app.get('*',function(req,res){ res.render('index'); }); } 

app.js

 angular.module('app',['ngResource','ngRoute']); angular.module('app').config(function($routeProvider,$locationProvider){ $locationProvider.html5Mode(true); $routeProvider .when('/',{ templateUrl:'/partials/main/main/',controllers:'mvMainCtrl'}); }); 

config.js

 var path=require('path'); var rootPath=path.normalize(__dirname + '/../../' ); module.exports={ development:{ db:'mongodb://localhost/multivision', rootPath:rootPath, port:process.env.PORT || 3030 }, production:{ db:'****', rootPath:rootPath, port:process.env.PORT || 80 } } 

main.jade

 //h1 This is a partial //h2{{myVar} .container .jumbotron h1 MultiVision p My first mean stack application .row .col-md-6 div(ng-include="'/partials/main/featured-courses'") .col-md-6 div(ng-include="'/partials/main/new-courses'") 

我得到的错误是

 Error: Failed to lookup view "../../public/app/[object Object]" in views directory "C:\Users\nyaconcepts\WebstormProjects\MultiVision\server/views" at Function.app.render (C:\Users\nyaconcepts\WebstormProjects\MultiVision\node_modules\express\lib\application.js:491:17) at ServerResponse.res.render (C:\Users\nyaconcepts\WebstormProjects\MultiVision\node_modules\express\lib\response.js:777:7) at Object.handle (C:\Users\nyaconcepts\WebstormProjects\MultiVision\server\config\routes.js:5:13) at next_layer (C:\Users\nyaconcepts\WebstormProjects\MultiVision\node_modules\express\lib\router\route.js:103:13) at Route.dispatch (C:\Users\nyaconcepts\WebstormProjects\MultiVision\node_modules\express\lib\router\route.js:107:5) at c (C:\Users\nyaconcepts\WebstormProjects\MultiVision\node_modules\express\lib\router\index.js:195:24) at Function.proto.process_params (C:\Users\nyaconcepts\WebstormProjects\MultiVision\node_modules\express\lib\router\index.js:251:12) at next (C:\Users\nyaconcepts\WebstormProjects\MultiVision\node_modules\express\lib\router\index.js:189:19) at SendStream.error (C:\Users\nyaconcepts\WebstormProjects\MultiVision\node_modules\express\node_modules\serve-static\index.js:74:37) at SendStream.EventEmitter.emit (events.js:95:17) 

       

网上收集的解决方案 "Mean Stack应用程序中的部分页面呈现问题"

您试图访问../../public/app/[object Object] ,这不是一个有效的path。 问题出在res.render('../../public/app/'+ req.params);req.params是一个对象,你需要从中提取一个包含部分名字的值。

这个从发电机angular度完整的片段 :

 /** * Send partial, or 404 if it doesn't exist */ exports.partials = function(req, res) { var stripped = req.url.split('.')[0]; var requestedView = path.join('./', stripped); res.render(requestedView, function(err, html) { if(err) { console.log("Error rendering partial '" + requestedView + "'\n", err); res.status(404); res.send(404); } else { res.send(html); } }); }; 

我遇到了同样的问题,这是为我解决了这个问题

  // Serve jade tempates from partials app.get('/partials/*', function(req, res){ console.log(req.params); res.render('../../public/app/' + req.params[0]); }); 

感谢@Roman的提示,了解发生了什么:)

这工作正常

  // Serve jade tempates from partials app.get('/partials/*', function(req, res){ console.log(req.params); res.render('../../public/app/' + req.params[0]); });