Utku
Utku

Reputation: 449

TypeError: Cannot read property 'get' of undefined (Node.js)

I am new in Node.js and I need help. I am trying to develop a simple program using express (^4.14.1), path (^0.12.7) and vash (^0.12.2) modules in Visual Studio. I have just created a couple of files that I show you below. homeController.js and index.js files are inside the controllers folder.

package.json

{
  "name": "the-board5",
  "version": "0.0.0",
  "description": "TheBoard5",
  "main": "server.js",
  "author": {
    "name": "Utku"
  },
  "dependencies": {
    "express": "^4.14.1",
    "path": "^0.12.7",
    "vash": "^0.12.2"
  }
}

server.js

var http = require("http");
var express = require("express");
var app = express();
var controllers = require("./controllers");
var port = process.env.port || 1337;

app.set("view engine", "vash");

controllers.init();

http.createServer(app).listen(port);

homeController.js

(function (homeController) {
    homeController.init = function (app) {
        app.get("/", function (req, res) {
            res.render("index", { title: "Express + Vash" });
        });
    };
})(module.exports);

index.js

(function (controllers) {
    var homeController = require("./homeController");
    controllers.init = function (app) {
        homeController.init(app);
    };
})(module.exports);

The problem is that I am getting this error.

C:\Users\Utku\documents\visual studio 2015\Projects\TheBoard5\TheBoard5\controll
ers\homeController.js:5
        app.get("/", function (req, res) {
           ^

TypeError: Cannot read property 'get' of undefined
    at Object.homeController.init (C:\Users\Utku\documents\visual studio 2015\Pr
ojects\TheBoard5\TheBoard5\controllers\homeController.js:5:12)
    at Object.controllers.init (C:\Users\Utku\documents\visual studio 2015\Proje
cts\TheBoard5\TheBoard5\controllers\index.js:5:24)
    at Object.<anonymous> (C:\Users\Utku\documents\visual studio 2015\Projects\T
heBoard5\TheBoard5\server.js:12:13)
    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)

Upvotes: 1

Views: 2878

Answers (1)

Ebrahim Pasbani
Ebrahim Pasbani

Reputation: 9396

You missed passing app in line

controllers.init();

It should be

controllers.init(app);

Upvotes: 3

Related Questions