Skip to content

Instantly share code, notes, and snippets.

@wuyanxin
Last active December 4, 2016 06:23
Show Gist options
  • Select an option

  • Save wuyanxin/239209cecc99ca0a3edacce610b3dbdc to your computer and use it in GitHub Desktop.

Select an option

Save wuyanxin/239209cecc99ca0a3edacce610b3dbdc to your computer and use it in GitHub Desktop.
A mini http-like server
const Server = require('./ihttp').Server;
const app = new Server().listen(3000);
const net = require('net');
const util = require('util');
const debug = util.debuglog('ihttp');
function Server() {
if (!(this instanceof Server)) return new Server(requestListener);
net.Server.call(this, { allowHalfOpen: true });
this.addListener('connection', connectionListener);
}
util.inherits(Server, net.Server);
var counter = 0;
function connectionListener(socket) {
debug('new connection is coming', ++counter);
socket.on('data', function (chunk) {
debug('request');
debug('chunk: ', chunk.toString('ascii', 0, chunk.length));
});
setTimeout(function () {
debug('response');
socket.end('hello world');
}, 1000);
}
module.exports = {
Server
};
@wuyanxin

wuyanxin commented Dec 4, 2016

Copy link
Copy Markdown
Author

ignore http request headers
no http response headers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment