shireef khatab shireef khatab - 3 months ago 11
Node.js Question

Route.all() requires callback functions but got a [object String])=> then showing 404 errros

I have recently started to look at node Js, my question is about express routing.

I have a dishRouter.js:

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

var bodyParser = require('body-parser');
dishRouter.use(bodyParser.json());

dishRouter //.route('/dishes')
.all('/dishes', function(req, res, next){
res.writeHead(200, {'Content-Type': 'application/json'});
next();
})
.get('/dishes', function(req, res, next){
res.end('will send all dishes to you');
})
.get('/dishes/:dishId', function(req, res, next){
res.end('will send the dish '+ req.params.dishId + ' to you');
})
.post('/dishes', function(req, res, next){
res.end('will add the dish '+ req.body.name + ' with details ' + req.body.description);
})
.put('/dishes/:dishId', function(req, res, next){
res.write('Updating the dish '+ req.params.dishId+ ' ');
res.end(' Updating the dish '+ req.body.name + ' with details '+ req.body.description);
})
.delete('/dishes', function(req, res, next){
res.end('Deleteing all dishes');
})
.delete('/dishes/:dishId', function(req, res, next){
res.end('Deleteing the dish '+ req.params.dishId);
});

module.exports = dishRouter;


And server.js:

var express = require('express');
var morgan = require('morgan');
var bodyParser = require('body-parser');
var host ='localhost';
var port = 3000;
var app = express();
app.use(morgan('dev'));
app.use(bodyParser.json());

app.use('/dishes', require('./dishRouter'));
app.use(express.static(__dirname + '/public'));

app.listen(port,host,function(){
console.log(`Server running at http://${host}:${port}`);
});


In a single file, it worked fine, but when I try to separate them out like above, it doesn't work, and now My terminal shows me this error:

Route.all() requires callback functions but got a [object String]


What am I doing wrong, please?

Update: 30.08.16 @ 23:38



I managed to fix the error at the terminal by NOT chaining .all() to .route()
so I`m doing this now:

dishRouter.route('/dishes');

dishRouter.all('/dishes', function(req, res, next){
res.writeHead(200, {'Content-Type': 'application/json'});
next();
})
.get('/dishes', function(req, res, next){
res.end('will send all dishes to you');
})
.get('/dishes/:dishId', function(req, res, next){
res.end('will send the dish '+ req.params.dishId + ' to you');
})
// ......... the rest as before..........


BUT: now i get 404 for all methods(get, post,put,delete):

Server running at http://localhost:3000
DELETE /dishes/0 404 219.103 ms - 24
GET /dishes/0 404 22.813 ms - 21
GET /dishes/ 404 1.743 ms - 20
GET / 200 7.699 ms - 130
GET /leaders 404 30.800 ms - 20
GET /leader 404 0.591 ms - 19
PUT /leaders/1 404 1.616 ms - 22
PUT /dishes/1 404 0.595 ms - 21
PUT /dishes/1 404 0.847 ms - 21
GET /dishes/1 404 0.857 ms - 21
GET /dishes 404 1.082 ms - 19
POST /dishes 404 0.679 ms - 20
POST /dishes 404 0.901 ms - 20
GET /dishes 404 2.847 ms - 19
POST /dishes 404 0.671 ms - 20


any idea what is wrong now? thank you..

Update on: 31/08/2016 @ 06:28am



I managed to retrieve the data, my mistake was using dishRouter.route('/dishes'); dishRouter.all('/dishes') ..... and so on.

now im doing dishRouter.route('/dishes'); dishRouter.all(/) ... and for params: dishRouter.get(/:dishId) .. and so on.

so here is my final file dishRouter.js:

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

var bodyParser = require('body-parser');
dishRouter.use(bodyParser.json());

dishRouter.route('/dishes');

dishRouter.all('/', function(req, res, next){
res.writeHead(200, {'Content-Type': 'application/json'});
next();
})
.get('/', function(req, res){
res.end('will send all dishes to you');
})
.get('/:dishId', function(req, res){
res.end('will send the dish ('+ req.params.dishId + ') to you');
})
.post('/', function(req, res){
res.end('will add the dish ('+ req.body.name + ') with details (' + req.body.description + 'about the dish)');
})
.put('/:dishId', function(req, res){
res.write('Updating the dish ('+ req.params.dishId+ ')');
res.end(' Updating the dish ('+ req.body.name + ') with details ('+ req.body.description + 'about the dish)');
})
.delete('/', function(req, res){
res.end('Deleteing all dishes');
})
.delete('/:dishId', function(req, res){
res.end('Deleteing the dish ('+ req.params.dishId + ')');
});

module.exports = dishRouter;


Thank you to all.

Answer

1-

I managed to fix the error at the terminal by NOT chaining .all() to .route() so I`m doing this now:

dishRouter.route('/dishes');

dishRouter.all('/dishes', function(req, res, next){
    res.writeHead(200, {'Content-Type': 'application/json'});
        next();
})
.get('/dishes', function(req, res, next){
    res.end('will send all dishes to you');
})
.get('/dishes/:dishId', function(req, res, next){
    res.end('will send the dish '+ req.params.dishId + ' to you');
}) 
   // ......... the rest as before..........

BUT: now i get 404 for all methods(get, post,put,delete):

    Server running at http://localhost:3000
    DELETE /dishes/0 404 219.103 ms - 24
    GET /dishes/0 404 22.813 ms - 21
    GET /dishes/ 404 1.743 ms - 20
    GET / 200 7.699 ms - 130
    GET /leaders 404 30.800 ms - 20
    GET /leader 404 0.591 ms - 19
    PUT /leaders/1 404 1.616 ms - 22
    PUT /dishes/1 404 0.595 ms - 21
    PUT /dishes/1 404 0.847 ms - 21
    GET /dishes/1 404 0.857 ms - 21
    GET /dishes 404 1.082 ms - 19
    POST /dishes 404 0.679 ms - 20
    POST /dishes 404 0.901 ms - 20
    GET /dishes 404 2.847 ms - 19
    POST /dishes 404 0.671 ms - 20

any idea what is wrong now? thank you.. it took me long until ....

2-

I managed to retrieve the data, my mistake was using dishRouter.route('/dishes'); dishRouter.all('/dishes') ..... and so on.

now im doing dishRouter.route('/dishes'); dishRouter.all(/) ... and for params: dishRouter.get(/:dishId) .. and so on.

so here is my final file dishRouter.js:

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

var bodyParser = require('body-parser');
dishRouter.use(bodyParser.json());

dishRouter.route('/dishes');

dishRouter.all('/', function(req, res, next){
    res.writeHead(200, {'Content-Type': 'application/json'});
        next();
})
.get('/', function(req, res){
    res.end('will send all dishes to you');
})
.get('/:dishId', function(req, res){
    res.end('will send the dish ('+ req.params.dishId + ') to you');
})
.post('/', function(req, res){
    res.end('will add  the dish ('+ req.body.name + ') with details (' + req.body.description + 'about the dish)');
})
.put('/:dishId', function(req, res){
    res.write('Updating the dish ('+ req.params.dishId+ ')');
    res.end(' Updating the dish ('+ req.body.name + ') with details ('+ req.body.description + 'about the dish)');
})
.delete('/', function(req, res){
    res.end('Deleteing all dishes');
})
.delete('/:dishId', function(req, res){
    res.end('Deleteing the dish ('+ req.params.dishId + ')');
});

module.exports = dishRouter;

Thank you to all.