0

I'm trying to structure my NodeJS API in a different way, but I'm getting the following error when trying to import my routes:

/Users/pato/Documents/nodejs-bp-api/node_modules/express/lib/router/index.js:139 debug('dispatching %s %s', req.method, req.url); ^

TypeError: Cannot read property 'method' of undefined

express.js file

.
.
app.use(cors());
app.use(config.api.prefix, apiRoutes());

API routes/index.js

const todo = require('../api/routes/todo');
const express = require('express');   
const apiRoutes = () => {
   const app = express.Router();
   todo(app);
   return app();
};

    module.exports = apiRoutes;

todo.js route

const todo = router => {
  router.get('/', function(req, res, next) {
    res.json({ msg: 'This is CORS-enabled for all origins!' });
  });
};

1 Answer 1

2

return app(); should just be return app; (with no parentheses). A router is called as a function by express when handling a request, not during setup.

Sign up to request clarification or add additional context in comments.

1 Comment

omg, i fixed right before you posted the answer haha thanks so much! that's what it was!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.