Mihaly
Mihaly

Reputation: 301

cannot get / in a simple node file

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);

Upvotes: 2

Views: 46

Answers (1)

khollenbeck
khollenbeck

Reputation: 16157

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

Upvotes: 2

Related Questions