nlr_p nlr_p - 2 months ago 9
Node.js Question

routes not calling the function in express

When i go with routes in my browser,it shows the results in my console but in network the server call is running for long time ,can someone suggest help/.............

My controller,

var express = require('express');
var router = express.Router();
var mysql = require('mysql');
var connection = mysql.createConnection({
// connectionLimit : 100, //important
host : 'localhost',
user : 'root',
password : 'root',
database : 'socialwiki'
});
connection.connect(function(error){
if(!!error){
console.log('error');

}else{
console.log('connected');
}
});
exports.getlist = function( req, res ) {
connection.query("SELECT * FROM profile",function(error,result,rows,fields){
if(!!error){
console.log('fail');
}else{
console.log(result);
}


});
}

I am trying to provide routes at my routes folder,
user.js

var express = require('express');
var router = express.Router();
var app = express();
var admin = require('../controller/user');
/* GET home page. */
router.get('/hm', function(req, res, next) {
res.render('index', { title: 'Express' });
});
router.get('/hm1', function(req, res, next) {
res.render('index', { title: 'Entertainment' });
});
router.get('/users', admin.getlist)
module.exports = router;


I am completely new to express js can someone suggest help please.........

Answer

You need to send the HTTP response, using res.send in express

For example:

exports.getlist = function(req, res) {
  connection.query("SELECT * FROM     profile", function(error, result, rows, fields) {
    if (!!error) {
      console.log('fail');
    } else {
      console.log(result);
      res.send(result);
    }
  });
};

The only change was to add res.send(result) as the last thing your script does.