Skip to content

Commit

Permalink
learned http 🌐
Browse files Browse the repository at this point in the history
  • Loading branch information
hk-skit committed Dec 11, 2017
1 parent b906385 commit 0f1e47e
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 0 deletions.
30 changes: 30 additions & 0 deletions http/chunked_response.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const http = require('http');

const PORT = 8000;
const HOST_NAME = '127.0.0.1';

const INTERVAL = 1000; // in ms

const server = http.createServer((req, res) => {

let left = 10;

const intervalId = setInterval(() => {

for (let i = 0; i < 100; i++) {
res.write(`${Date.now()} `);
}
left -= 1;
res.write('\n');
if (left === 0) {
clearInterval(intervalId);
res.end('All done');
}

}, INTERVAL);

});

server.listen(PORT, HOST_NAME, () => {
console.log(`Listening at ${HOST_NAME}:${PORT}`);
});
39 changes: 39 additions & 0 deletions http/hello_world.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const http = require('http');

const PORT = 8000;
const HOST_NAME = '127.0.0.1';

const server = http.createServer((req, res) => {

req.on('data', (data) => {
console.log('Request body', data);
});

res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');

// shorthand for above two statements
// res.writeHead(200, {
// 'Content-Type': 'text/plain'
// });


res.write('Hello World!!\n');
// Can also be writtn as
// res.end('Hello World!!');

// We can extract some useful information from request object.
res.write(`Request url: ${req.url}\n`);
res.write(`Request Method: ${req.method}\n`);
res.write('Request Headers:\n');
res.write(JSON.stringify(req.headers, null, 4));
res.end();

res.on('error', (error) => {
console.log('Something went wrong', error);
});
});

server.listen(PORT, HOST_NAME, () => {
console.log(`Listening at ${HOST_NAME}:${PORT}`);
});
49 changes: 49 additions & 0 deletions http/static_file.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const http = require('http');
const path = require('path');
const fs = require('fs');

const PORT = 8000;
const HOST_NAME = '127.0.0.1';

const server = http.createServer((request, response) => {

const file = path.normalize(`.${request.url}`);
console.log('Trying to serve ', file);

const reportError = (error) => {
console.log(error);
response.writeHEAD(500);
return response.end('Internal server error.');
};

fs.exists(file, (exists) => {

if (!exists) {
response.writeHead(404);
return response.end('Not found.');
}

fs.stat(file, (error, stat) => {

if (error) {
return reportError(error);
}

if (stat.isDirectory()) {
response.writeHead(403);
return response.end('Forbidden');
}

const rs = fs.createReadStream(file);
rs.on('error', reportError);
response.writeHead(200);
rs.pipe(response);
});

});

});

server.listen(PORT, HOST_NAME, () => {
console.log(`Listening at ${HOST_NAME}:${PORT}`);
});

0 comments on commit 0f1e47e

Please sign in to comment.