Monday, 22 May 2017

Node JS : Calling Http Service under Angular JS resolve

I am new to node js .

Initially when page is loaded with localhost:3000

index.html is called and redirects to allUsers.html

app.js

var express = require('express');
var path = require('path');
var app = express();
path = require('path'),
index = require('./routes/index');

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

app.get('/', function(req, res)
{
        res.sendFile(path.join(__dirname + '/admin_4_angularjs/index.html'));
});
module.exports = app;
var port = 3000;
app.listen(port, function()
{
        console.log('Listening on ' + port);
});

main.js

// UI Bootstrap
.state('allUsers',
{
        url: "/allUsers.html",
        templateUrl: "views/allUsers.html",
        data:
        {
                pageTitle: 'Users List'
        },
        controller: "allUsersController",
        resolve:
        {
                allUserslist: function($http)
                {
                        return $http.get("/users").then(function(response)
                        {
                                return response.data;
                        })
                }
        }
})

index.js

var express = require('express');
var mongodb = require('mongodb');
var router = express.Router();
var app = express();
var MONGODB_URI = 'mongodb://xxx.xxx.1.23:27017/RELRE';
var db;
var coll;
mongodb.MongoClient.connect(MONGODB_URI, function(err, database) {
  if(err) throw err;
  db = database;
});
router.get('/users', function(req, res) {
 var collectionInfo = db.collection("UsersInfo");
            collectionInfo.find({}).toArray(function(err, employees) {
               res.status(200).json({'myCollection' : employees});
            });
});

module.exports = router;

I get 404 under browser console .

http://localhost:3000/users 404 (Not Found)

Could you please let me know how to resolve this issue ?



via Pawan

No comments:

Post a Comment