1

routes.js

var categories = require('../controllers/categories.server.controller');

module.exports = function(app) {

app.get('/',function(request,response){
        return response.send("Welcome");
    });

    app.route('/')
        .get(function(request, response) {
            return response.send('Rayees');
        });
        .post(categories.create);

    app.route('/:categoryId')
        .get(categories.read)
        .put(categories.update)
        .delete(categories.delete)

    app.param('categoryId', categories.categoryId)

};

app.js file

'use strict'

var express = require('express');

var path = require('path');

var favicon = require('serve-favicon');

var logger = require('morgan');

var cookieParser = require('cookie-parser');

var bodyParser = require('body-parser');

var app = express(),

    mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/pressworld');

var category = require('./server/routes/category.server.routes')(app);

app.use(logger('dev'));

app.use(bodyParser.json());

app.use(bodyParser.urlencoded({
    extended: false
}));

app.use(cookieParser());

app.use('/api/category', category);

I am getting this error

TypeError: Router.use() requires middleware function but got a undefined
    at Function.use (D:\Development\reduxpress\node_modules\express\lib\router\index.js:458:13)
    at EventEmitter.<anonymous> (D:\Development\reduxpress\node_modules\express\lib\application.js:219:21)
    at Array.forEach (native)
    at EventEmitter.use (D:\Development\reduxpress\node_modules\express\lib\application.js:216:7)
    at Object.<anonymous> (D:\Development\reduxpress\app.js:49:5)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (D:\Development\reduxpress\bin\www:7:11)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
Nick D
  • 1,483
  • 1
  • 13
  • 22
Rayees
  • 57
  • 3
  • 15
  • 1
    `at Object. (D:\Development\reduxpress\app.js:49:5)` You're not showing your full `app.js`. Whatever is on line *49* is causing the error. – Lars de Bruijn Apr 15 '16 at 12:29
  • Did you have the following line in your app.js `require('your route.js path')(app)` – Subburaj Apr 15 '16 at 12:32
  • i am exporting app from aap.js module.exports = app; and requiring in www var app = require('../app'); – Rayees Apr 15 '16 at 13:05

1 Answers1

0

Make use of express.Router and return router obj and use it in express middleware.

Please modify your routes.js and make use of router.get , router.post instead of app.

module.exports = function(app) {

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


    router.get('/',function(request,response){
        return response.send("Welcome");
    });

    return router;

};
Hiren S.
  • 2,793
  • 15
  • 25
  • why can't i do it my way, why to use router. actually i want to chain routes – Rayees Apr 15 '16 at 13:14
  • You can find the reason here: http://stackoverflow.com/questions/28305120/differences-between-express-router-and-app-get – Hiren S. Apr 15 '16 at 13:16