Skip to content

Instantly share code, notes, and snippets.

@daniel12fsp
Last active October 25, 2018 16:43
Show Gist options
  • Save daniel12fsp/a9ad247313dec2e368eefed02a985191 to your computer and use it in GitHub Desktop.
Save daniel12fsp/a9ad247313dec2e368eefed02a985191 to your computer and use it in GitHub Desktop.
Simple implementation of HTTP in Node
const net = require('net');
const server = net.createServer(function (socket) {
socket.on('data', function (data) {
const header = data.toString();
console.log("--------start request--------");
console.log(header);
console.log("--------end request--------");
let response = "HTTP/1.1 200 OK\r\n";
body = "<h1> HELLO WORLD &#128521; </h1>";
response += 'Content-Length: ' + body.length + '\r\n';
response += 'Content-Type: text/html; charset=utf-8\r\n';
response += '\r\n';
response += body;
socket.write(response);
socket.pipe(socket);
})
});
server.listen(8080, 'localhost');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment