Advertisement
Guest User

static-files.js

a guest
May 27th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require('fs');
  2. const path = require('path');
  3. const url = require('url');
  4.  
  5. function getContentType(url) {
  6.     //
  7. }
  8.  
  9. module.exports = (req, res) => {
  10.     req.pathname = req.pathname || url.parse(req.url).pathname;
  11.  
  12.     if(req.pathname.startsWith('/content/') && req.method === 'GET') {
  13.         let filePath = path.normalize(path.join(__dirname, `..${req.pathname}`));
  14.  
  15.         fs.readFile(filePath, (err, data => {
  16.             if(err) {
  17.                 res.WriteHead(404, {
  18.                     'Content-Type': 'text/plain'
  19.                 });
  20.             }
  21.  
  22.             res.write('Resource not found!');
  23.             res.end();
  24.             return;
  25.         }
  26.  
  27.         res.writeHead(200, {
  28.             'Content-Type': getContentType(req.pathname)
  29.         });
  30.     ));
  31.     } else {
  32.         return true;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement