Skip to content

Instantly share code, notes, and snippets.

@maretekent
Created February 2, 2018 08:45
Show Gist options
  • Save maretekent/b9e78a5ca1d470e766d3b582360f81ef to your computer and use it in GitHub Desktop.
Save maretekent/b9e78a5ca1d470e766d3b582360f81ef to your computer and use it in GitHub Desktop.
node js server
var http = require('http');
var server = undefined;
function HttpServer(config) {
this.port = config.port;
}
HttpServer.prototype.start = function (fn) {
server = http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({ data: 'Some data'}));
}).listen(this.port, fn);
console.log('Server started at [%s]', this.port);
return this;
};
HttpServer.prototype.stop = function (fn) {
server.close();
if (fn) {
fn();
}
};
exports.HttpServer = HttpServer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment