Last active
September 1, 2021 02:01
-
-
Save matthewoestreich/dd2ef57c99c53e3f3bec5436ab0e6455 to your computer and use it in GitHub Desktop.
Vanilla node.js server : POC -> gain control routes + methods : https://stackoverflow.com/questions/69006743/unable-to-send-post-request-to-node-js-http-webserver-from-anything-other-than-l/69006944#69006944
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
const http = require("http"); | |
const fs = require("fs"); | |
const fspath = require("path"); | |
const url = require("url"); | |
const server = http.createServer(async (request, response) => { | |
const path = url.parse(request.url).pathname; | |
switch (path) { | |
/** | |
* @route '/home' description for route | |
* @methods (GET) | |
*/ | |
case "/home": { | |
switch (request.method) { | |
case "GET": | |
default: { | |
try { | |
const html = await fs.readFileSync(fspath.resolve("./home.html")); | |
response.writeHead(200); | |
response.write(html); | |
} catch (e) { | |
console.error(e); | |
response.writeHead(500); | |
response.write("unable to read home html : " + e); | |
} | |
response.end(); | |
} | |
} | |
} | |
/** | |
* @route '/update' | |
* @methods (POST) | |
*/ | |
case "/update": { | |
switch (request.method) { | |
case "POST": { | |
console.log("in /update POST"); | |
const buffers = []; | |
for await (const chunk of request) { | |
buffers.push(chunk); | |
} | |
const data = Buffer.concat(buffers).toString(); | |
console.log(JSON.parse(data)); | |
response.end(); | |
} | |
default: { | |
response.end(); | |
} | |
} | |
} | |
default: { | |
response.end(); | |
} | |
} | |
}); | |
server.listen(34567); | |
const serverAddress = server.address(); | |
const serverIp = serverAddress.address === "::" ? "localhost" : serverAddress.address; | |
console.log(`Server listening at : ${serverIp}:${serverAddress.port} : go to http://localhost:34567/home`); |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Client</title> | |
<!-- script src="https://unpkg.com/axios/dist/axios.min.js"></script --> | |
</head> | |
<body> | |
<textarea id="stream" rows="8" cols="80" oninput="update(this.value)"></textarea> | |
<script> | |
async function update(v) { | |
let data = { | |
text: v, | |
}; | |
await send(data); | |
} | |
async function send(d) { | |
try { | |
const res = await fetch("/update", { | |
method: "POST", | |
body: JSON.stringify(d), | |
}); | |
console.log(res); | |
} catch (e) { | |
console.error(e); | |
} | |
// ~ | |
// REMOVED : sorry, fetch was just easier.. | |
// ~ | |
// axios.post( | |
// "http://<the server IP>:34567", | |
// JSON.stringify(d) | |
// ).then(res => { | |
// // do nothing | |
// }).catch(error => { | |
// console.error(error); | |
// }); | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment