Wednesday 3 May 2017

404 when making a request to express route

I am trying to understand express and how it handles routes.

I have a domain set up with the following structure

/
  app.js
    /public_html
      index.html

In app.js, I set up my express server:

let app = express();

app.post('/list', (request, response) => {
  //get data then...
  response.send(data)
});

app.use(express.static('public_html'))

app.listen(3000, function(){
  console.log('listening');
});

I run the app with node app.js

Then, in index.html in the public_html directory, I am trying to request the data. I just did a simple:

fetch('/list').then(function(response) {
  console.log(response)
})

But I get a 404 as the response.

I'm a bit confused about a couple of things:

  1. My web server (Apache / Ubuntu) is set up to serve html out of the public_html directory by default. Does that mean my whole node app structure needs to be moved into the public_html folder and the actual html moved into a static folder or something?

  2. What about the port? The node app listens on port 3000 - but I'm not sure how (or if) to make a request to that port specifically.

  3. Route path - I am posting to /list but should it be ../list?

I haven't yet found a configuration for this app that works yet. Any help would be appreciated.



via mheavers

No comments:

Post a Comment