如何res.json和res.render同时,通过蒙戈分贝angularjs? [英] How to res.json and res.render at the same time, pass mongo db to angularjs?

查看:1019
本文介绍了如何res.json和res.render同时,通过蒙戈分贝angularjs?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用前preSS,附带玉模板引擎。
app.js

I use express, which comes with jade template engine. app.js

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

为每个模板,我需要有 res.render 有HTML渲染头,如果我不使用 res.render ,模板/页将不会显示。 路线/ index.js

for each template, I will need to have res.render to have the html header rendered, if I don't use res.render, the template/page won't show. routes/index.js

router.get('/', function(req, res, next) {
    res.render('index', { title: 'Bookshop' });
});

router.get('/data', function(req, res, next) {
    res.render('data', { title: 'Bookshop | Categories' });
});

router.get('/items', function(req, res, next) {
    res.render('items', { title: 'Bookshop | Items' });
});

app.js 我有我的MongoDB正确设置。

in app.js I've got my MongoDB set up correctly.

var mongojs = require('mongojs');
var db = mongojs('book', ['books']);

我所要达到的目的是MongoDB的数据库传递到角,然后循环玉文件。 myscript.js

app.controller('appCtrl', function ($scope, $http) {
   $http.get('/data').then(function(res){
        console.log(res.data);
        $scope.books = res.data;
    });
});

data.jade 文件看起来像这样

p(ng-repeat="item in books")
    span {{item.name}}
    span {{item.barcode}}
    span {{item.price}}

现在我停留在如何将数据库传递到 myscript.js 。我不能做这种方式的 app.js

Right now I am stuck on how to pass database to myscript.js. I am unable to do this way in app.js

router.get('/data', function(req, res, next) {
    var db = req.db;
     db.zero.find(function (err, docs) {
        res.json(docs)
    });
});

原因是玉模板被使用,需要使用 res.render 来获取页面显示正确,但如果我用路由器获得('/数据'功能(REQ,资源,下一个){
        res.render('数据',{标题:书店|类别'});
    });
我无法通过数据库。

the reason is jade template is being used, and need to use res.render to get the page showed correctly, however if I use router.get('/data', function(req, res, next) { res.render('data', { title: 'Bookshop | Categories' }); }); I am unable to pass database.

我明白我不能使用res.render和res.json该同一时间。是有通数据库角度的解决方案?或者我有玉转换为HTML?我相信,如果我使用的HTML文件,我可以使用 res.json

I understand I am unable to use res.render and res.json the at same time. is there a solution for pass database to angular? or do I have to convert jade to html? I am sure if I use html file I can use res.json

感谢

推荐答案

没有。 res.render()呈现一个模板,并将其发送给客户端。响应内容类型使用时,头文件 res.render()的text / html 为例。但 res.json() JSON 格式将数据发送到客户端,从而内容型头的响应将是应用程序/ JSON

No. res.render() renders a template and send it to the client. The response Content-Type header when using res.render() would be text/html for example. But res.json() sends data in json format to the client, thus the Content-Type header for the response will be application/json.

更新

您已经以达到你想要的创建两条路线。第一条路线会使您玉模板。而为了在角器从服务器检索数据,JSON,你需要使用另一条路线。例如:

You have to create two routes in order to achieve what you want. The first route will render your jade template. And in order to retrieve data from server as JSON in Angular controller you need to use another route. For example:

router.get('/data', function(req, res, next) {
    res.render('data', { title: 'Bookshop | Categories' });
});

router.get('/data.json', function(req, res, next) {
    var db = req.db;
    db.myCollection.find(function (err, docs) {
        if(err) {
            // Handle error
        } else {
            res.json({title: 'Data', 'mydata': docs});
        }
    });
});

而在角控制器使用第二条路线如下图所示:

And in the Angular controller you use the second route like below:

$http.get('/data.json').then(function(res) {
    console.log(res.data);
});

这篇关于如何res.json和res.render同时,通过蒙戈分贝angularjs?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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