1

I am trying to run my node app. But I am getting an error, which I am not able to understand. please any one help me to understand this?

here is my code :

var express = require("express"),
  app = express(),
  path = require("path");

app.get("/", function( req, res ) {
    res.sendfile( path.join(__dirname + '/index.html'));
});

var adminRouter = express.Router();

adminRouter.get('/', function(req, res) {
    res.send('I am the dashboard!');
});

app.use("/admin", adminRouter);

app.listen(process.env.PORT, process.env.IP);
console.log("basic app listeners!");

the error I am getting is :

adminRouter.get('/', function(req, res) {
           ^

TypeError: Cannot read property 'get' of undefined
    at Object.<anonymous> (/home/ubuntu/workspace/server.js:16:12)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
    at node.js:990:3

Can any one help me? I am running my app in cloud9.

thanks in advance!!

3
  • Did you try: express.Router().route() . It is not depreciated. Commented Mar 10, 2017 at 6:58
  • 1
    Your code looks correct and I cannot reproduce the problem. What version of node and express are you using? Commented Mar 10, 2017 at 7:04
  • did you created run configuration for express in cloud9 Commented Mar 10, 2017 at 7:06

2 Answers 2

3

Your express version less than +4 , probably version 3. Try

npm uninstall express --save 

Then re-install.

adminRouter.get('/', function(req, res) {
res.send('I am the dashboard!');
});

try it like this as well.

 adminRouter.route('/').get(function(req,res){
res.json({'hello there from main route!'});
});
Sign up to request clarification or add additional context in comments.

2 Comments

best bet is to delete your node_modules folder. Change the express version to latest and re run npm install on the updated json package. cloud 9 does not support express version +4.x. That's the main reason for not executing the code correctly.
4.15.2 is the latest for express.
2

Actually this is express version problem.

express.Router(); is supported on version 4.x and cloud 9 support by default 3.x

change your package.json

"express": "^4.15.2",

and delete node_module folder then run

npm install

Comments

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.