RJorns
RJorns

Reputation: 281

Keep getting TypeError that comes back as undefined

I am trying to get my Node.js server up and running locally, but it keeps coming back as an ERROR specifically:

/Users/rogerjorns/Desktop/templateassignment/node_modules/express/lib/router/index.js:458
      throw new TypeError('Router.use() requires a middleware function but got a ' + gettype(fn))
      ^

TypeError: Router.use() requires a middleware function but got a undefined
    at Function.use (/Users/rogerjorns/Desktop/templateassignment/node_modules/express/lib/router/index.js:458:13)
    at Function.<anonymous> (/Users/rogerjorns/Desktop/templateassignment/node_modules/express/lib/application.js:220:21)
    at Array.forEach (<anonymous>)
    at Function.use (/Users/rogerjorns/Desktop/templateassignment/node_modules/express/lib/application.js:217:7)
    at Object.<anonymous> (/Users/rogerjorns/Desktop/templateassignment/app.js:16:5)
    at Module._compile (internal/modules/cjs/loader.js:868:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:879:10)
    at Module.load (internal/modules/cjs/loader.js:731:32)
    at Function.Module._load (internal/modules/cjs/loader.js:644:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:931:10)`

I am using Node.js, Express and EJS template. Here is my code for the routes:

app.js

const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.set('view engine', 'ejs');

const indexData = require('./routes/index');
const userRoutes = require('./routes/users');

app.use(bodyParser.urlencoded({extended: false}));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/index', indexData.routes);
app.use(userRoutes);

app.use((req, res, next) => {
  res.status(404).render('404', { pageTitle: 'Page Not Found', path:'/' });
});

app.listen(5000);

path.js

const path = require('path');

module.exports = path.dirname(process.mainModule.filename);

index.js

const path = require('path');

const express = require('express');

const rootDir = require('../util/path');
const indexData = require('./index');

const router = express.Router();

router.get('/', (req, res, next) => {
  const users = indexData.users;
  res.render('index', {
    users: users,
    pageTitle: 'Main',
    path: '/',
    hasUsers: users.length > 0,
    activeIndex: true,
    indexCSS: true
  });
});

// exports.routes = router;
module.exports = router;

users.js

const path = require('path');

const express = require('express');

const rootDir = require('../util/path');

const router = express.Router();

const users = [];

// GET
router.get('/users', (req, res, next) => {
  res.render('users', {
    pageTitle: 'Users Page',
    path: '/index/add-users',
    formsCSS: true,
    indexCSS: true,
    usersCSS: true,
    activeUsers: true
  });
});

// POST
router.post('/add-users', (req, res, next) => {
  users.push({ title: req.body.title });
  res.redirect('/');
});

exports.routes = router;
exports.users = users;

It keeps coming up with that error. I have been pouring over this for a long time and am still kind of new at this. Something I have tried is to replace 'module.exports = route;' in index.js with 'exports.router;', and that seems to take away the error but my page then just comes up as 404 error no matter what I type in. What am I doing wrong? Any insight here would be great.

Upvotes: 0

Views: 55

Answers (1)

Turtlefight
Turtlefight

Reputation: 10680

You switched up indexData and userRoutes:

app.use('/index', indexData.routes);
app.use(userRoutes);

should be

app.use('/index', indexData);
app.use(userRoutes.routes);

Upvotes: 1

Related Questions