Skip to content

Instantly share code, notes, and snippets.

@xhsdnn
Last active February 2, 2023 02:21
Show Gist options
  • Save xhsdnn/0db61f02c078aecd445337a34f1cb84e to your computer and use it in GitHub Desktop.
Save xhsdnn/0db61f02c078aecd445337a34f1cb84e to your computer and use it in GitHub Desktop.
const http = require('http');
let server;
function onRequest(req, res) {
console.log('[' + this.name + ']', req.method, req.url);
res.setHeader('Access-Control-Allow-Origin', '*');
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}
function onListening() {
console.log('[' + this.name + '] Listening at http://' + this.address().address + ':' + this.address().port + '/');
}
server = http.createServer();
server.name = 'TestServer';
server.on('request', onRequest);
server.on('listening', onListening);
server.listen(8084, '127.0.0.1'); // ipv4: http://127.0.0.1:8084/
// server.listen(8084, '::'); // ipv6: http://[::]:8084/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment