-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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}`); | ||
}); |
This file contains 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
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}`); | ||
}); |
This file contains 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
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}`); | ||
}); |