Skip to content

Instantly share code, notes, and snippets.

@melikhov-dev
Last active October 20, 2017 19:50
Show Gist options
  • Save melikhov-dev/db5f67953e370c9108cf7506577696e4 to your computer and use it in GitHub Desktop.
Save melikhov-dev/db5f67953e370c9108cf7506577696e4 to your computer and use it in GitHub Desktop.
const server = http2.createSecureServer(options);
const commonCSS = fs.readFileSync('common.css');
server.on('stream', (stream, headers) => {
console.log(`${headers[':method']} ${headers[':path']}`);
const parsedUrl = url.parse(headers[':path']);
let pathname = `.${parsedUrl.pathname}`;
const ext = path.parse(pathname).ext;
if (headers[':path'] === '/index.html') {
stream.pushStream({ ':path': 'common.css' },
(pushStream) => {
pushStream.respond({
':status': 200,
'content-type': 'text/css'
});
pushStream.end(commonCSS, () => {});
});
const indexPath = path.join(__dirname, pathname);
const readStream = fs.createReadStream(indexPath);
readStream.on('open', function () {
stream.respond({
'content-type': mimeType[ext] || 'text/plain',
':status': 200
});
readStream.pipe(stream);
});
} else {
// остальная статика
}
});
server.listen(8800);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment