{"size": 1340, "ext": "js", "max_stars_repo_path": "ExpressJS Fundamentals/04.Exercise Web Server and Development Tools/MovieDB/ExerciseNodeMovieDB/handlers/static-handler.js", "max_stars_repo_name": "RadoslavStanchev/JS-Web-January-2019", "max_stars_repo_stars_event_min_datetime": "2018-03-08T03:56:49.000Z", "max_stars_repo_stars_event_max_datetime": "2018-08-22T15:02:45.000Z", "max_issues_repo_path": "ExpressJS Fundamentals/04.Exercise Web Server and Development Tools/MovieDB/ExerciseNodeMovieDB/handlers/static-handler.js", "max_issues_repo_name": "RadoslavStanchev/JS-Web-January-2019", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "ExpressJS Fundamentals/04.Exercise Web Server and Development Tools/MovieDB/ExerciseNodeMovieDB/handlers/static-handler.js", "max_forks_repo_name": "RadoslavStanchev/JS-Web-January-2019", "max_forks_repo_forks_event_min_datetime": "2018-08-24T10:34:16.000Z", "max_forks_repo_forks_event_max_datetime": "2018-08-24T10:34:16.000Z"}
{"max_stars_count": 2, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 22.3333333333, "max_line_length": 77, "alphanum_fraction": 0.4701492537}
const fs = require('fs');
const faviconPath = './public/images/favicon.ico';

let fileType = (dataString) => {
      let dataTypes = {
          '.css': 'text/css',
          '.js': 'application/javascript',
          '.png': 'image/png',
          '.jpeg': 'image/jpeg',
          '.ico': 'image/x-icon'
      };

    for (let type in dataTypes) {
        if(dataString.endsWith(type)){
            return dataTypes[type];
        }
    }
};

let favHandler = (req, res) => {
    fs.readFile(faviconPath, (err, data) => {
        if (err) {
            console.warn(err);
            return;
        }

        res.writeHead(200, {
            'content-type': fileType(req.pathname)
        });

        res.write(data);
        res.end();
    });
};
let resData = (req, res) => {
    fs.readFile('.' + req.pathname, (err, data) => {
        if(err){
            console.warn(err);
            return;
        }
        res.writeHead(200, {
            'content-type': fileType(req.pathname)
        });
        res.write(data);
        res.end();

    });
};

module.exports = (req, res) => {
    if (req.pathname === '/favicon.ico' && req.method === 'GET') {
        favHandler(req, res);

    } else if (req.pathname.startsWith('/public/')  && req.method === 'GET'){
        resData(req, res);

    } else{
        return true;
    }
};