Skip to content

Instantly share code, notes, and snippets.

@MrKotov
Last active October 17, 2022 15:56
Show Gist options
  • Save MrKotov/3690757b95ba93a8b79744f58b800b9b to your computer and use it in GitHub Desktop.
Save MrKotov/3690757b95ba93a8b79744f58b800b9b to your computer and use it in GitHub Desktop.
Hello world NodeJS Server
const http = require('http');
const HOST = '127.0.0.1';
const PORT = +process.argv[2] || 3001;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ success: true, message: "Hello world!" }));
})
server.listen(PORT, HOST).on('error', (err) => {
console.error(`Something went wrong: ${err}`);
}).on('listening', () => {
console.log(`"Hello World" Node Server running at http://${HOST}:${PORT}`);
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment