FACN4 / stars_of_the_north_w4

Autosuggest project
0 stars 0 forks source link

router and handlers #41

Open i2xzy opened 6 years ago

i2xzy commented 6 years ago

your handlers are your own function so they can expect any arguments you want. If the only thing you're doing with an argument is extracting something from it, then you can do that extracting before you call the function.

const { homeHandler, staticHandler } = require('./handlers.js')
const { url } = request.url;
  if (url === '/') {
    homeHandler(response);
  } else if (url.indexOf('/public') === 0) {
    staticHandler(url, response);
  }
const homeHandler = (response) => {
  ...
  response.end(file);
};
const staticHandler = (url, response) => {
  ...
  // use url to get correct file
  ...
  response.end(file);
}