Sandeepan Nath Sandeepan Nath - 1 year ago 57
Node.js Question

Express - nodejs app spinning indefinitely when a module is browsed

Note - I am newbie trying to write my first application using express.
I followed a video tutorial and did not follow any e-book or document tutorial to learn. Hence finding it difficult to find out the thing I am missing by reading an ebook now.


I have the following files -

app.js
/src/routes/testRoutes.js
/src/controllers/testController.js


Following is my app.js file -

var express = require('express');
var app = express();
var port = 5000;

var testRouter = require('./src/routes/testRoutes');
app.use('/Test',testRouter);

app.get('/',function(req,res){
res.send("Hello world");
});

app.listen(port,function(err){
console.log("Running port on "+port);
});


testRoutes.js

var express = require('express');
var testRouter = express.Router();

var router = function()
{
var testController = require('../controllers/testController')(null);
testRouter.route('/hello').get(testController.hello);

return testRouter;
};


module.exports = router;


testController.js

//Will use revealing module pattern
var testController = function(botService)
{
var hello = function(req,res){
console.log("Inside bot controller");
};

return {
hello:hello
}
}

module.exports = testController;


If I browse
http://localhost:5000/
after running
node app
I can see a
Hello world
but if I browse
localhost:5000/Test
, it spins indefinitely.
For now, I would just like to display some message on the browser directly from the controller file.

Update

With the following modification to the router file, testRoutes.js (removing the function), and adding a
res.send()
to the controller, I am able to make it work (stop the spinning and display the message) -

var express = require('express');
var testRouter = express.Router();
var testController = require('../controllers/testController');

testRouter.get('/world',testController.hello);
module.exports = testRouter;


But, I am still not able to make it work using the function in the route file, which can take parameters. I went through the links https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/routes and http://expressjs.com/en/guide/routing.html.

Update 2

With reference to @SatishPatel's answer, changing the contents of my controller file to the following format worked for me -

function something()
{
return 5;
}
//Following works but above does not
module.exports = {
hello : function(req, res){
res.send('Get a book '+something())
},
getAllBooks : function(req, res){
res.send('Get all book');
}
}


However, I would like to make it work while I use the revealing module pattern, as mentioned in my question description above. If I use the following in my controller -

//Will use revealing module pattern

var testController = function()
{
function something()
{
return 5;
}

var hello = function(req,res){
//console.log("Inside bot controller");
res.send("Hello world");
};

return {
hello:hello
}
}

module.exports = testController;


Following is the error I am getting on
npm start
-

D:\lab\nodejs_scraper\node_modules\express\lib\router\route.js:202
throw new Error(msg);
^

Error: Route.get() requires callback functions but got a [object Undefined]
at Route.(anonymous function) [as get] (D:\lab\nodejs_scraper\node_modules\express\lib\router\route.js:202:15)
at Object.<anonymous> (D:\lab\nodejs_scraper\src\routes\testRoutes.js:7:28)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.require (module.js:497:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (D:\lab\nodejs_scraper\app.js:6:18)


An acceptable solution to the question would be one which solves this.

Update 3

Marking as resolved because the spinning problem got solved after adding a
res.send()
in the controller.

Answer Source

Probably you can separate your controller and routes as follows:

server.js

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var port =5000;
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
app.use('/',routes);
app.listen(port,function(err){
    console.log("Running port on "+port);
});

route.js

var express = require('express');
var bookCtrl = require('../controllers/bookCtrl');
var router = express.Router();
router.route('/').get(function(req,res){
    res.send('you are on root url');
});
router.route('/book').get(bookCtrl.getBook);
router.route('/allbooks').get(bookCtrl.getAllBooks);
/* Write all you apis here and control them in controller*/
module.exports = router;

bookCtrl.js

module.exports = {
getBook : function(req, res){
   res.send('Get a book')
},
getAllBooks : function(req, res){
   res.send('Get all book');
}
}

Folder structure as in image folder structure

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download