Last active
October 20, 2017 19:50
-
-
Save melikhov-dev/db5f67953e370c9108cf7506577696e4 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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