Last active
December 24, 2019 05:41
-
-
Save iamssen/baf3199159863e4dd7b22149ec39dd0a to your computer and use it in GitHub Desktop.
Understanding HTTP using Node.js "net" 2. HTTP Server
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
import { createServer, Server, Socket } from 'net'; | |
import fetch from 'node-fetch'; | |
const port: number = 9903; | |
// --------------------------------------------- | |
// server | |
// --------------------------------------------- | |
const server: Server = createServer((clientSocket: Socket) => { // 2. this is the fetch client below | |
console.log(`[server] connected client: ${JSON.stringify(clientSocket.address())}`); | |
clientSocket.on('data', data => { // 3. receive data from fetch client | |
console.log(`[server] received data from client:`); | |
console.log(data.toString().split('\r\n')); | |
}); | |
}); | |
server.listen(port, () => { | |
console.log(`[server] opened server: ${JSON.stringify(server.address())}`); | |
}); | |
// --------------------------------------------- | |
// client | |
// --------------------------------------------- | |
fetch(`http://localhost:${port}`); // 1. connect to server and send data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment