Skip to content

Instantly share code, notes, and snippets.

@justinvdm
Last active January 22, 2020 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save justinvdm/d07a668a9f04af12f614c3ff5d93c515 to your computer and use it in GitHub Desktop.
Save justinvdm/d07a668a9f04af12f614c3ff5d93c515 to your computer and use it in GitHub Desktop.
const http = require("http");
const CSS_DELAY = 5000;
const html = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<link href="/style.css" rel="stylesheet">
<title>rarily rar rar</title>
</head>
<body>
<h1>Hi</h1>
<script src="/script.js"></script>
</body>
</html>
`;
const js = `
console.log('js loaded');
`;
const css = `
body {
background: #f00;
}
`;
const handleIndex = (req, res) => {
res.write(html);
res.end();
};
const handleCss = (req, res) => {
setTimeout(() => {
res.write(css);
res.end();
}, CSS_DELAY);
};
const handleJs = (req, res) => {
res.write(js);
res.end();
};
const handle = (req, res) => {
if (req.url.includes("css")) {
console.log("css requested");
handleCss(req, res);
} else if (req.url.includes("js")) {
console.log("js requested");
handleJs(req, res);
} else {
handleIndex(req, res);
}
};
http.createServer(handle).listen(3000);
{
"name": "__css-blocking.js",
"version": "1.0.0",
"description": "",
"main": "__css-blocking.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node __css_blocking.js"
},
"keywords": [],
"author": "",
"license": "MIT"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment