Created
May 26, 2023 13:05
-
-
Save stepankuzmin/c6fc1de271707740b06ba5dcfbdae755 to your computer and use it in GitHub Desktop.
Express request to Toxy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const zlib = require('zlib'); | |
const toxy = require('toxy'); | |
const express = require('express'); | |
const proxy = toxy(); | |
// Gzip CSS file | |
const cssContent = 'body { background-color: purple; }'; | |
const gzippedCssContent = zlib.gzipSync(cssContent); | |
// Gzip JS file | |
const jsContent = 'console.log("Hello, world!");'; | |
const gzippedJsContent = zlib.gzipSync(jsContent); | |
// Middleware to serve gzipped CSS and JS files | |
proxy.use((req, res) => { | |
res.setHeader('Content-Encoding', 'gzip'); | |
if (req.url.endsWith('.js')) { | |
res.setHeader('Content-Type', 'application/javascript'); | |
res.write(gzippedJsContent); | |
} | |
if (req.url.endsWith('.css')) { | |
res.setHeader('Content-Type', 'text/css'); | |
res.write(gzippedCssContent); | |
} | |
res.end(); | |
}); | |
// Start the proxy server | |
proxy.listen(4000, () => { | |
console.log('Proxy server is running on http://localhost:4000'); | |
}); | |
// start express server wich serves single html from memory | |
const app = express(); | |
app.get('/', (req, res) => { | |
res.send(` | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<link rel="stylesheet" href="http://localhost:4000/styles.css"> | |
</head> | |
<body> | |
<script src="http://localhost:4000/script.js"></script> | |
</body> | |
</html> | |
`); | |
}); | |
app.listen(3000, () => { | |
console.log('Express server is running on http://localhost:3000'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment