角和Ex preSS路由 [英] Angular and Express routing

查看:94
本文介绍了角和Ex preSS路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过很多角度-EX preSS种子和种制定出他们是如何工作的。
我遇到的问题是: 1)我想用EJS-当地人模板。 2)。如何正确配置服务器端和客户端的路由。而且,输入URL,如 /约,不产生错误时:不能/让

角的 app.js 包含:

  //角的东西$ routeprovider.when('/',{
 templateUrl:索引,
 控制器:IndexCtrl
});
$ routeprovider.when('/约',{
 templateUrl:'谐音/约,
 控制器:IndexCtrl
});

前preSS的应用程序,JS 包含:

  app.get('/',routes.index);
app.get('/约,routes.about);

文件夹航线包含'index.js':

  exports.index =功能(REQ,RES){
  res.render('指数',{名字:你好});
};exports.about =功能(REQ,RES){
  res.render('谐音/约');
};

视图文件夹包含 index.ejs

 <! -  HTML头/导航栏在这里 - >
< D​​IV NG-视图>< / DIV>

和内部意见文件夹是谐音文件夹:
查看/谐音/

index.ejs:

 < H1>指数< / H1>

about.ejs:

 < H1>关于< / H1>


解决方案

这些路由添加到你的前preSS服务器

  app.get('/谐音/:文件名',routes.partials);
app.use(routes.index);

然后在 routes.js

  exports.partials =功能(REQ,RES){
  变种文件名= req.params.filename;
  如果回报(文件名!); //可能要改变这种
  res.render(谐音/+文件名);
};exports.index =功能(REQ,RES){
  res.render('指数',{消息:你好!});
};

发出请求到的谐音/索引时,这将确保前preSS回报呈现模板的谐音/约

下面是一个要点: https://gist.github.com/4277025

I've been through many Angular-express seeds and kind of worked out how they work. The problem I am having is: 1). I would like to use ejs-locals for templating. 2). How to configure correctly the routing of the server-side and client-side. And also, when entering a URL such as /about, not to generate the error: cannot /get

angular app.js contains:

// angular stuff

$routeprovider.when('/', {
 templateUrl: 'index',
 controller: IndexCtrl
});
$routeprovider.when('/about', {
 templateUrl: 'partials/about',
 controller: IndexCtrl
});

express app,js contains:

app.get('/', routes.index);
app.get('/about', routes.about);

routes folder contains 'index.js':

exports.index = function(req, res){
  res.render('index',{name:"Hello"});
};

exports.about = function (req, res) {
  res.render('partials/about');
};

Views folder contains index.ejs:

<!--HTML head/navigation bar here-->
<div ng-view></div>

and inside views folder is a partials folder: (Views/partials/)

index.ejs:

 <h1>Index</h1>

about.ejs:

<h1>About</h1>

解决方案

Add these routes to your express server

app.get('/partials/:filename', routes.partials);
app.use(routes.index);

Then in routes.js

exports.partials = function(req, res){
  var filename = req.params.filename;
  if(!filename) return;  // might want to change this
  res.render("partials/" + filename );
};

exports.index = function(req, res){
  res.render('index', {message:"Hello!!!"});
};

This will make sure that express returns rendered templates when making requests to partials/index and partials/about.

Here's a gist: https://gist.github.com/4277025

这篇关于角和Ex preSS路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆