Skip to content

Instantly share code, notes, and snippets.

View httpServer.js
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 httpServer.js
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 server.js
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 server.js
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
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 async_exec_time.js
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 async-promise.js
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)
}