Skip to content

Instantly share code, notes, and snippets.

@euske
Created April 28, 2025 21:02
Show Gist options
  • Save euske/5d1a076f8758e5ac94a46a6cd6572416 to your computer and use it in GitHub Desktop.
Save euske/5d1a076f8758e5ac94a46a6cd6572416 to your computer and use it in GitHub Desktop.
Simple HTTP Server in NodeJS
#!/usr/bin/env node
const http = require('http');
const port = parseInt(process.env.PORT || '8080');
const contentType = process.env.CONTENT_TYPE || 'text/html';
const body = process.env.BODY ?? '<h1>Hello world!</h1>';
const server = http.createServer((request, response) => {
console.info(request.socket?.remoteAddress, request.method, request.url, request.httpVersion);
console.info(request.headers);
response.writeHead(200, {'Content-Type': contentType, 'Access-Control-Allow-Origin': '*'});
response.end(body);
});
console.info(`listening on :${port}...`);
server.listen(port);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment