Created
February 2, 2018 08:45
-
-
Save maretekent/b9e78a5ca1d470e766d3b582360f81ef to your computer and use it in GitHub Desktop.
node js server
This file contains hidden or 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 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