-
-
Save geonnave/be1356922e85d0524c1cba3b208ab270 to your computer and use it in GitHub Desktop.
This file contains 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
const net = require('net'); | |
const readline = require('readline'); | |
const PORT = 8000; | |
const REQUEST = `\ | |
GET / HTTP/1.1 | |
`; | |
const client = new net.Socket(); | |
client.connect(PORT, 'localhost', () => { | |
console.log('Connected. Press enter to send the request.'); | |
}); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
rl.question('Press Enter to send request...', (answer) => { | |
console.log('Sending request:'); | |
console.log(REQUEST); | |
client.write(REQUEST); | |
rl.close(); | |
}); | |
client.on('data', (data) => { | |
console.log(data.toString()); | |
}); | |
client.on('end', () => { | |
console.log('Connection closed'); | |
process.exit(0); | |
}); | |
client.on('error', (err) => { | |
console.log(`Error: ${err}`); | |
process.exit(1); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment