JDillon522
JDillon522

Reputation: 19686

Express routes: .get() requires callback functions but got a [object Object]

Ok, this should be easy for somebody to point out.

I checked the other similar questions and none helped.

I'm trying to move all my routes to a separate routes.js file. In it I have:

module.exports = function (app) {

  var user = {
      list : require('./routes/user.js')
    } 
  , index = {
      index : require('./routes/index.js')
    } 


  app.get('/', function(request, response){
    response.send('You made it to the home page.')
  });

  app.get('/users', user.list);
}

And in my app.js file I have this:

var register_routes = require('./routes.js')
register_routes(app)

My index route works fine, but it kicks back on app.get('/users', user.list); with this error:

.get() requires callback functions but got a [object Object]

This is an out of the box express app so theres not too much to describe.

Thanks.

EDIT: Per request, here is what is in ./routes/user.js :

exports.list = function(req, res){
  res.send("respond with a resource");
};

Upvotes: 26

Views: 73703

Answers (4)

user10261875
user10261875

Reputation:

After so many time seeking around in web, I found something.

First of all, You instantiate the code like this on another file, (e.g.: humancomms.ts):

import express from 'express';

export function shout(request: express.Request, response: express.Response, next: () => void) {
    response.send('Shout so loud!');
}

export function speak(request: express.Request, response: express.Response, next: () => void) {
    response.send('Speak less loud!');
}

What exactly this code does? Nobody knows.(Hehe, just kiddin')

This make a middleware functions to separate from main server file to... organize, of course.

And how We can use it? Just like this (inside your server file):

const shout = require('./humancomms').shout;
const speak = require('./humancomms').speak;

app.use('/shout', shout);
app.use('/speak', speak);

This code takes all middlewares funcs and executes when some path is called.

This not solve all problems like multi path to same type, as if You want call differently intensities of shout ('/shout/high?asd=asd', '/shout/low?asd=asd'), but there is a catch You can try on secondary file:

import express from 'express';

export function shout(request: express.Request, response: express.Response, next: () => void) {

    if (request.path.includes('/high')) {
        response.send('Shout so loud!');
    } else if (request.path.includes('/low')) {
        response.send('Really shout?');
    }
}

Look at this good article about:

TypeScript Express tutorial #1. Middleware, routing, and controllers (https://wanago.io/2018/12/03/typescript-express-tutorial-routing-controllers-middleware/)

and the official documentation website:

Writing middleware for use in Express apps (http://expressjs.com/en/guide/writing-middleware.html)

Upvotes: 0

Nikhil
Nikhil

Reputation: 3

If you are using router in your application for all routing purpose,

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

var index = require('./index');


/* GET home page. */
router.get('/', index.list);

module.exports = router;

then in your index.js file, just do

router.list = function(req, res){
   res.send("respond with a resource"); 
};

Upvotes: 0

t.niese
t.niese

Reputation: 40872

You export an object with the key list having the your function as value.

So to access your function you would need to do this require('./routes/user.js').list

Or with your code user.list.list.

To solve this you have two possibilities.

Either write:

var user = {
  list : require('./routes/user.js').list
}

Or:

module.exports = function(req, res){
   res.send("respond with a resource");
};

EDIT

If your routes/user.js will probably later look like this:

module.exports.list = function(req, res){
   res.send("respond with a resource");
};

module.exports.delete = function(req, res){
   res.send("delete user");
};

If yes then you can just write it that way in your routes.js:

var user = require('./routes/user.js');

Upvotes: 20

Miguel Q.
Miguel Q.

Reputation: 607

I think what you want is:

module.exports = function (app) {

  var user = {
      list : function(request, response){  
                    require('./routes/user.js');
            } 
}
    } 
  , index = {
      index : function(request, response){ 
          require('./routes/index.js') 
        }
    } 


  app.get('/', function(request, response){
    response.send('You made it to the home page.')
  });

  app.get('/users', user.list);
}

In this way give a callback to the route and this callback execute the require.

Upvotes: 1

Related Questions