Advertisement
xapu

Untitled

Oct 6th, 2017
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const http = require('http')
  2. const fs = require('fs')
  3. const Minifier = require('minify-html-stream').Minifier
  4. const minifyCss = require('minify-css-stream')
  5. const streamer = require('string-to-stream')
  6.  
  7. const port = 1111
  8. const htmlPath = './starting.html'
  9. const cssPath = './css.css'
  10.  
  11. let zlib = require('zlib')
  12. let gzip = zlib.createGzip()
  13.  
  14. let cssString = `h1{
  15.   background: orange;
  16.   font-family: sans-serif;
  17.   font-size: 15;
  18.   padding: 15px;
  19. }
  20.  
  21. p{
  22.   color: blue;
  23.   background: greenyellow;
  24.   font-size: 15px;
  25. }
  26. `
  27.  
  28. let htmlString = `<!DOCTYPE html>
  29. <html lang="en">
  30.     <head>
  31.         <title></title>
  32.         <meta charset="UTF-8">
  33.         <link rel="stylesheet" type="text/css" href="theme.css">
  34.     </head>
  35.     <body>
  36.         <h1>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Mollitia, saepe!</h1>
  37.         <p>Lorem ipsum dolor sit, amet consectetur adipisicing elit. Inventore nesciunt cumque quam hic placeat, nemo laborum quos porro commodi iste beatae soluta quibusdam vel repellat! Facere reiciendis aperiam voluptatibus nulla iste praesentium asperiores aliquid in rerum officiis debitis placeat adipisci molestiae, deserunt iusto consequuntur perferendis dolore expedita ab? Praesentium asperiores temporibus quia, adipisci aperiam, numquam nam odio nostrum error modi optio, alias minima? Rem aliquam consequatur autem voluptates porro mollitia dolorem sed, doloremque nam iusto provident molestiae sapiente. Animi corrupti sint similique eos asperiores voluptate perferendis illo fugit ab quia nihil quaerat quis ipsa eaque neque dolorum, sit, adipisci incidunt?</p>
  38.         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Sunt, facere!</p>
  39.     </body>
  40. </html>`
  41.  
  42. http
  43.   .createServer((req, res) => {
  44.     if (req.url.endsWith('/')) {
  45.  
  46.       res.writeHead(200,{
  47.         'Content-Encoding':'gzip',
  48.         'Content-Type':'text/html'
  49.       })
  50.  
  51.       streamer(htmlString).pipe(new Minifier()).pipe(gzip).pipe(res)
  52.  
  53.       // fs.readFile(htmlPath,(err,data)=>{
  54.       //     res.end(data)
  55.       // })
  56.     }
  57.     if (req.url.endsWith('.css')) {
  58.       res.writeHead(200,{
  59.         'Contetnt-Type':'text/css'
  60.       })
  61.       streamer(cssString).pipe(minifyCss()).pipe(res)
  62.       // fs.readFile(cssPath,(err,data)=>{
  63.       //     res.end(data)
  64.       // })
  65.     }
  66.   })
  67.   .listen(port)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement