Skip to content

Instantly share code, notes, and snippets.

@Clivern
Last active April 18, 2022 13:35
Show Gist options
  • Save Clivern/922eca631e146bea40abd4f8b4350ab8 to your computer and use it in GitHub Desktop.
Save Clivern/922eca631e146bea40abd4f8b4350ab8 to your computer and use it in GitHub Desktop.
Learning NodeJs

HTTP Web Server

const server = require('http')
const SERVER_PORT = process.argv[2]  || 8000

server.createServer(function (req, res) {
    res.writeHead(200, {'content-type': 'text/plain'});
    res.end("Hello, World\n");
}).listen(SERVER_PORT);

console.log(`\x1b[33m Running on localhost:${SERVER_PORT}\x1b[0m`);
@Clivern
Copy link
Author

Clivern commented Apr 18, 2022

TCP Server

const n = require('net')

n.createServer(function(conn){
	console.log('Connected')

	conn.on('data', function(data){
		console.log('data from ' + conn.remoteAddress)
		conn.write('Repeating: ' + data)
	})

	conn.on('close', function(){
		console.log("Client closed connection")
	})
}).listen(8000)

console.log("listen on port 8000")

// $ nc 127.0.01 8000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment