Skip to content

Not very dry code in the handler #29

Description

@finnhodgkin
const getData = (req, res, url) => {
  getDataDb(url.query, (err, result) => {
    if (err) handleError(err, res);
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify(result));
  });
};
const getDataAll = (req, res, url) => {
  getDataAllDb(url.query, (err, result) => {
    if (err) handleError(err, res);
    res.writeHead(200, { 'Content-Type': 'application/json' });
    res.end(JSON.stringify(result));
  });
};
const postData = (req, res, url) => {
  console.log('in postData handler.');
  // console.log('postdata urlobject is ', url);
  postDataDb(url.query, (err, result) => {
    if (err) handleError(err, res);
    res.writeHead(200, { 'Content-Type': 'application/javascript' });
    res.end(result);
  });
};

Is the same as:

const query = (req, res, url, queryFunction) => {
  queryFunction(url.query, (err, result) => {
    if (err) return handleError(err, res);
    res.writeHead(200, { 'Content-Type': 'application/javascript' });
    res.end(JSON.stringify(result));
  });
};

const getData = (req, res, url) => query(req, res, url, getDataDb);
const getDataAll = (req, res, url) => query(req, res, url, getDataAllDb);
const postData = (req, res, url) => query(req, res, url, postDataDb);

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions