2

I am trying to do a simple node / angular example of a book got. However i get an error when loading the page: cannot GET /

Anyone knows what i am doing wrong? Thanks

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use('/', express.static('./static')).
    use('/images', express.static( '../images')).
    use('/lib', express.static( '../lib'));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var days=['Monday', 'Tuesday', 'Wednesday',
          'Thursday', 'Friday'];
var serviceDays = days.slice(0);
app.get('/reset/days', function(req, res){
  serviceDays = days.slice(0);
  res.json(serviceDays);
});
app.post('/remove/day', function(req, res){
  if (serviceDays.length > 2){
    serviceDays.splice(serviceDays.indexOf(req.body.day), 1);
    console.log(days);
    res.json(serviceDays);
  }else {
    res.json(400, {msg:'You must leave 2 days'});
  }
});
app.listen(8081);
4
  • 1
    which error you get? that's a key information to understand what you are doing wrong... Commented Feb 9, 2016 at 15:33
  • Might want to look this over. expressjs.com/en/guide/routing.html Usually you get that error because of an incorrect route. Commented Feb 9, 2016 at 15:33
  • Also just to be sure.. Can you post the URL that you are trying to go to? Commented Feb 9, 2016 at 15:34
  • I go to localhost:8081 Commented Feb 9, 2016 at 15:40

1 Answer 1

2

I don't see you doing a .get() for this path /, which is the same as localhost:8081. Try doing something like this in your code.

app.get('/', function(req, res){
  serviceDays = days.slice(0);
  res.json(serviceDays);
});

This is the same as what you have here

app.get('/reset/days', function(req, res){
  serviceDays = days.slice(0);
  res.json(serviceDays);
});

So if you go to localhost:8081 or localhost:8081/reset/days, you should get the same result. What the message cannot GET / is saying is that there is no route set up for the requested url.

Also note: It looks like you are using app.use('/', express.static('./static')) in your code. But it looks like this is just defining all of your static routes for "static", "images", and "lib".

Here is some more info on .use vs .get

  1. Difference between app.use and app.get in express.js
  2. http://expressjs.com/en/4x/api.html#app.use
  3. http://expressjs.com/en/4x/api.html#app.get
Sign up to request clarification or add additional context in comments.

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.