Getting an error on incorrect routes

250
July 07, 2017, at 12:02 PM

When I run node server I am getting an error and cannot figure out the root cause. I am pasting the error below. I would really appreciate some help as I have spent a lot of time going through stack overflow posts and trying to debug the issue. Thank you.

ERROR:

C:\Users\desktop\studentSearch\node_modules\express\lib\router\index.js:458
      throw new TypeError('Router.use() requires middleware function but got a ' + gettype(fn));
      ^
TypeError: Router.use() requires middleware function but got a undefined
    at Function.use (C:\Users\Monica\desktop\studentSearch\node_modules\express\lib\router\index.js:458:13)
    at Function.<anonymous> (C:\Users\Monica\desktop\studentSearch\node_modules\express\lib\application.js:220:21)
    at Array.forEach (native)
    at Function.use (C:\Users\Monica\desktop\studentSearch\node_modules\express\lib\application.js:217:7)
    at Object.<anonymous> (C:\Users\Monica\desktop\studentSearch\server.js:46:5)
    at Module._compile (module.js:571:32)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:488:32)
    at tryModuleLoad (module.js:447:12)
    at Function.Module._load (module.js:439:3)
    at Module.runMain (module.js:605:10)
    at run (bootstrap_node.js:420:7)
    at startup (bootstrap_node.js:139:9)
    at bootstrap_node.js:535:3

CODE

// ******************************************************************************
// *** Dependencies
// =============================================================
var express = require("express");
var exphbs = require("express-handlebars");
var bodyParser = require("body-parser");
var methodOverride = require("method-override");
    jwtExp = require("express-jwt");
    tokenSecret = require("./tokensecret.js");
    cookieParser = require("cookie-parser");
// Sets up the Express App
// =============================================================
var app = express();
var PORT = process.env.PORT || 8080;
//cookie parser
app.use(cookieParser(tokenSecret));
// Set Handlebars as the default templating engine.
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");
// Requiring our models for syncing
var db = require("./models");
// Sets up the Express app to handle data parsing
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: "application/vnd.api+json" }));
// Override with POST having ?_method=DELETE
app.use(methodOverride("_method"));
// Routes =============================================================
require("./routes/html-routes.js")(app);
var apiRoutes = require("./routes/api-routes.js")(app);
var authRoutes = require("./routes/auth-routes.js");
// API MIDDLEWARE
// app.use("/api", jwtExp({ secret: tokenSecret }));
app.use("/api", apiRoutes);

// AUTH MIDDLEWARE
app.use("/auth", authRoutes);
app.use("/auth", express.static("./public"));
// USER MIDDLEWARE
app.get("/", jwtExp({
  secret: tokenSecret,
  getToken: function fromCookie(req) {
    if (req.signedCookies) {
      return req.signedCookies.jwtAuthToken;
    }
    return null;
  },
  credentialsRequired: false
}), function(req, res, next) {
  if (req.user) {
    next();
  } else {
    res.redirect("/auth/login");
  }
});
app.use("/", apiRoutes);
app.use(express.static("./public"));

// Syncing our sequelize models and then starting our express app
db.sequelize.sync({}).then(function() {
    app.listen(PORT, function() {
        console.log("App listening on PORT " + PORT);
    });
});
Answer 1

The issue was having the (app) in the definition. See below.

var apiRoutes = require("./routes/api-routes.js")(app); 
READ ALSO
Issue with node.js app stopping on AWS

Issue with node.js app stopping on AWS

I have a nodejs app that pings a url and then sends a message to Slack if it gets an error code

229
Store server state on crash

Store server state on crash

My server has a random number that I generate every 1/100 of a secondIf it crashes, it's very important I store that number

269
my meteor web app has failed to start on ubuntu droplet

my meteor web app has failed to start on ubuntu droplet

after several configuration for deploying meteor app, i realised that nodejs app is not runningor failed to run

360
My Slack bot expands shortened URL into incorrect website

My Slack bot expands shortened URL into incorrect website

I have a Slack botMy bot listens to shortened URLs posted on Slack groups and expands them

291