Pawan
Pawan

Reputation: 32331

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 ?

Upvotes: 0

Views: 60

Answers (1)

Ian
Ian

Reputation: 490

You required the router you created, but you need to tell express to use it.

The simplest way would be to add app.use(index) to app.js after your GET route.

app.get('/', function(req, res)
{
        res.sendFile(path.join(__dirname + '/admin_4_angularjs/index.html'));
});
app.use(index)

Express looks for matching routes top down, so when looking for your /users route, it will first check your / route and then check the routes the router you included.

However, if you plan to have several routers, I also recommend providing a base path to app.use indicating that the router should handle all routes that start with that base path. For example, in your particular case:

app.use('/users', index) // may also want to give the router a more meaningful name

And then modify you router in index.js like so:

router.get('/', function(req, res) { // within the router, '/users' is now assumed to be the base path
    // Logic for this route
});

Upvotes: 4

Related Questions