Created
April 28, 2025 21:02
-
-
Save euske/5d1a076f8758e5ac94a46a6cd6572416 to your computer and use it in GitHub Desktop.
Simple HTTP Server in NodeJS
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
#!/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