需要帮助将node.js连接到angular.js

时间:2015-07-29 13:34:44

标签: javascript angularjs node.js mongodb

好的,我知道mongodb,node.js和angular.js很少.....并且在某些时候我受到了打击,所以我故意需要帮助。我知道node.js代码但是可以&#t; t了解如何通过angular.js撤回它。请写一下angular.js代码如何从这个给定的node.js代码中获取数据。如果我的node.js代码中有任何错误,请通知我。

var express = require('express');
app = express(); // Web framework to handle routing requests
cons = require('consolidate'); // Templating library adapter for Express
MongoClient = require('mongodb').MongoClient; // Driver for connecting to MongoDB
routes = require('./routes'); // Routes for our application
bodyParser = require('body-parser');

MongoClient.connect('mongodb://127.0.0.1/blog', function(err, db) { "use strict"; if(err) throw err;

    // Register our templating engine
    app.engine('html', cons.swig);
    app.set('view engine', 'html');
    app.set('views',__dirname + '/views');

    // Express middleware to populate 'req.cookies' so we can access cookies
    app.use(express.cookieParser());

    // Express middleware to populate 'req.body' so we can access POST variables
    app.use(express.bodyParser());

    // The main page of the blog
    app.get('/', function(req, res, next) {
           Post.find(function(err, posts){
                  if(err){ return next(err); }

                  res.json(posts);
            });
    });     


    app.listen(8082);
    console.log('Express server listening on port 8082');

});

1 个答案:

答案 0 :(得分:0)

帖子没有定义?

也许

var Post = mongoose.model('Post');

然后使用ngResource从api

获取数据
app.factory('allPosts', function($resource){
return $resource('/');});