kab
kab

Reputation: 91

javascript error TypeError: Cannot read property 'post' of undefined

I have created a MEAN stack web application by following the tutorial http://start.jcolemorrison.com/building-an-angular-and-express-app-part-2/ After adding the signup.js as below,

var express = require('express');
var router = express.Router();
router.post('/', function (req, res) {

I am getting below error :

router.post('/', function (req, res) {
      ^
TypeError: Cannot read property 'post' of undefined

Please guide me.

Upvotes: 2

Views: 1709

Answers (1)

brandonscript
brandonscript

Reputation: 72905

Not sure what that tutorial is trying to get you to do, but try and see if this works:

var express = require('express');
var app = express();
app.post('/', function (req, res) {
    res.json({hello: "world"});
});

If it doesn't, then you've probably got a syntax error somewhere that's preventing router from initializing.

If it does work, then my only thought is that since Router() is new to Express 4.X, perhaps you're not using a 4.X version of Express? You can check by doing:

npm list express

And if necessary, upgrade by doing:

npm install express

Upvotes: 2

Related Questions