-
-
Save Shrekie/4fa8bf58fac59e988fb2d9330fcb86e5 to your computer and use it in GitHub Desktop.
Simple Node Server
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
<script> | |
let data = { postMessage: "Hello Postworld" }; | |
fetch("/api/posting", { | |
method: "POST", | |
headers: { "Content-Type": "application/json" }, | |
body: JSON.stringify(data), | |
}).then(async (res) => { | |
console.log(res); | |
console.log("response:", await res.json()); | |
}); | |
fetch("/api/getting", { | |
method: "GET", | |
headers: { "Content-Type": "application/json" }, | |
}).then(async (res) => { | |
console.log(res); | |
console.log("response:", await res.json()); | |
}); | |
</script> |
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 http = require("http"); | |
var fs = require("fs"); | |
const PORT = process.env.PORT || 8080; | |
const getReqData = (req) => { | |
return new Promise((resolve, reject) => { | |
try { | |
let body = ""; | |
req.on("data", (chunk) => { | |
body += chunk.toString(); | |
}); | |
req.on("end", () => { | |
resolve(body); | |
}); | |
} catch (error) { | |
reject(error); | |
} | |
}); | |
}; | |
const serveFile = (filePath, res) => { | |
fs.readFile(__dirname + filePath, (err, data) => { | |
if (err) { | |
res.writeHead(404); | |
res.end(JSON.stringify(err)); | |
return; | |
} | |
res.writeHead(200); | |
res.end(data); | |
}); | |
}; | |
const server = http.createServer(async (req, res) => { | |
if (req.url === "/" && req.method === "GET") { | |
// APPLICATION.HTML | |
return serveFile("/application.html", res); | |
} | |
if (req.url === "/api/getting" && req.method === "GET") { | |
// GETTING | |
res.writeHead(200, { "Content-Type": "application/json" }); | |
return res.end(JSON.stringify({ getMessage: "Hello Getworld" })); | |
} | |
if (req.url === "/api/posting" && req.method === "POST") { | |
// POSTING | |
const post_data = await getReqData(req); | |
console.log(JSON.parse(post_data)); | |
res.writeHead(200, { "Content-Type": "application/json" }); | |
return res.end(post_data); | |
} | |
// NOT FOUND | |
res.writeHead(404, { "Content-Type": "application/json" }); | |
res.end(JSON.stringify({ message: "Route not found" })); | |
}); | |
server.listen(PORT, () => { | |
console.log(`server started on port: ${PORT}`); | |
}); |
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
https://www.digitalocean.com/community/tutorials/how-to-install-node-js-on-ubuntu-20-04 | |
https://www.howtogeek.com/261383/how-to-access-your-ubuntu-bash-files-in-windows-and-your-windows-system-drive-in-bash/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment