2

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.

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

The code you provided above does work.And when i checked the express version it came out to be 3.4.0.Please inform how whould i update the express version on my application
Upgrade to a 4.X version of Express then: run npm install express. The latest as of writing this is 4.13.3.

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.