Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const http = require('http')
- const fs = require('fs')
- const Minifier = require('minify-html-stream').Minifier
- const minifyCss = require('minify-css-stream')
- const streamer = require('string-to-stream')
- const zlib = require('zlib')
- const gzip = zlib.createGzip()
- const gzip2 = zlib.createGzip()
- const port = 1111
- const htmlPath = './starting.html'
- const cssPath = './css.css'
- let cssString = `h1{
- background: orange;
- font-family: sans-serif;
- font-size: 15;
- padding: 15px;
- }
- p{
- color: blue;
- background: greenyellow;
- font-size: 15px;
- }
- `
- let htmlString = `<!DOCTYPE html>
- <html lang="en">
- <head>
- <title></title>
- <meta charset="UTF-8">
- <link rel="stylesheet" type="text/css" href="theme.css">
- </head>
- <body>
- <h1>Lorem ipsum, dolor sit amet consectetur adipisicing elit. Mollitia, saepe!</h1>
- <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>
- <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Sunt, facere!</p>
- </body>
- </html>`
- let func1 = (req,res)=>{
- console.log(req.url)
- res.writeHead(200,{
- 'Content-Encoding':'gzip',
- 'Content-Type':'text/html'
- })
- streamer(htmlString).pipe(new Minifier()).pipe(gzip).pipe(res)
- }
- let func2 = (req,res)=>{
- console.log(req.url)
- res.writeHead(200,{
- 'Content-Encoding':'gzip',
- 'Content-Type':'text/css'
- })
- streamer(cssString).pipe(gzip2).pipe(res)
- }
- http
- .createServer((req, res) => {
- if (req.url.endsWith('/')) {
- func1(req,res)
- }else if (req.url.endsWith('.css')) {
- func2(req,res)
- }else{
- res.writeHead(404)
- res.write()
- res.end()
- }
- })
- .listen(port)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement