View async-promise.js
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
const fs = require('fs'); | |
const readFileAsArray = function(file, cb = () => {}) { | |
return new Promise((resolve, reject) => { | |
fs.readFile(file, function(err, data) { | |
if (err) { | |
return reject(err) | |
cb(err) | |
} |
View async_exec_time.js
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
const fs = require('fs'); | |
const EventEmitter = require('events') | |
class WithTime extends EventEmitter { | |
execute(asyncFunc, ...args) { | |
console.time('execute'); | |
this.emit('begin'); | |
asyncFunc(...args, (err, data) => { | |
if (err) { | |
return this.emit('error', err); |
View server.js
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
const server = require('net').createServer() | |
server.on('connection', socket => { | |
console.log('client connected') | |
socket.write('welcom client\n') | |
socket.on('data', data => { | |
console.log('data is:', data) | |
socket.write('data is: ') | |
socket.write(data) |
View server.js
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
const server = require('net').createServer() | |
let sockets = {} | |
let counter = 0 | |
server.on('connection', socket => { | |
socket.id = counter++ | |
sockets[socket.id] = socket | |
console.log('client connected') | |
socket.write('welcome client\n') |
View server.js
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
var express = require('express') | |
var fs = require('fs') | |
var app = express() | |
app.get('/', function(req, res) { | |
// contents = fs.readFileSync('index.html') | |
res.sendFile(__dirname + '/index.html', (err)=> { | |
if (err) { | |
res.status(500).send(err) |
View httpServer.js
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
server = require('http').createServer() | |
const fs = require('fs'); | |
server.on('request', (req, res) => { | |
switch (req.url) { | |
case '/home': | |
case '/about': | |
res.writeHead(200, {'content-type' : 'text/plain'}) | |
res.end(fs.readFileSync(`.${req.url}.html`)); | |
case '/': |
View httpServer.js
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
server = require('http').createServer() | |
const fs = require('fs'); | |
const data = {'hi' : 'buy'} | |
server.on('request', (req, res) => { | |
switch (req.url) { | |
case '/api': | |
res.writeHead(200, {'Content-Type' : 'application-json' }) | |
res.end(JSON.stringify(data)) | |
break; |
View writable.js
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
// const { Writable } = require('stream') | |
// const outStream = new Writable({ | |
// write(chunk, encoding, callback) { | |
// console.log(chunk.toString()); | |
// callback(); | |
// } | |
// }) | |
// process.stdin.pipe(outStream) |
View readable.js
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
const {Readable} = require('stream') | |
const inStream = new Readable( { | |
read(size) { | |
this.push(String.fromCharCode(this.currentCharCode++)) | |
if (this.currentCharCode > 90) { | |
this.push(null) | |
} | |
} | |
}); |
View readable.js
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
const {Readable} = require('stream') | |
const inStream = new Readable( { | |
read(size) { | |
setTimeout(() => { | |
this.push(String.fromCharCode(this.currentCharCode++)) | |
if (this.currentCharCode > 90) { | |
this.push(null) | |
} | |
}, 100) |
OlderNewer