Created
October 21, 2019 18:43
-
-
Save sabicalija/18b96d10e92a928d7ae250b53410def2 to your computer and use it in GitHub Desktop.
Node.js - Example: HTTP
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 welcomePage = `<html> | |
<head><title>My First Page</title></head> | |
<body><h1>Hello from my Node.js Server!</h1></body> | |
</html>`; | |
const defaultPage = `<html> | |
<head><title>My First Page</title></head> | |
<body> | |
<h1>This Page is not available!</h1> | |
<h2>Take me <a href="/index.html">home</a>.</h2> | |
</body> | |
</html>`; | |
const todoPage = `<html> | |
<head><title>My First Page</title></head> | |
<body> | |
<form action="/todo/add" method="POST"> | |
<div><input type="text" name="todo" autofocus/></div> | |
<div><button type="submit">Create</button></div> | |
</form> | |
</body> | |
</html>`; | |
/* Create a http server */ | |
const server = http.createServer((request, response) => { | |
const { url, method } = request; | |
if (url === "/") { | |
response.writeHead(301, { Location: "/index.html" }); | |
return response.end(); | |
} else { | |
response.setHeader("Content-Type", "text/html"); | |
if (url === "/index.html") { | |
return response.end(welcomePage); | |
} else if (url === "/todo.html") { | |
return response.end(todoPage); | |
} else if (url === "/todo/add" && method === "POST") { | |
let data = ""; | |
request.on("data", chunk => (data += chunk.toString())); | |
request.on("end", () => { | |
addTodo(data); | |
response.writeHead(302, { Location: "/" }); | |
return response.end(); | |
}); | |
return; | |
} | |
} | |
response.statusCode = 404; | |
response.end(defaultPage); | |
}); | |
/* Add a new todo note */ | |
function addTodo(data) { | |
const filename = "./todo.txt"; | |
const note = processUserData(data); | |
fs.exists(filename, exists => { | |
if (exists) updateNotes(filename, note); | |
else createNotes(filename, note); | |
}); | |
} | |
function processUserData(data) { | |
return decodeURIComponent(data) | |
.replace(/(.*?)=(.*)/g, (m, key, value) => value) | |
.replace(/\+/g, " "); | |
} | |
function updateNotes(filename, note) { | |
fs.readFile(filename, "utf-8", (error, content) => { | |
if (!error) createNotes(filename, [content, note].join("\n")); | |
else console.error(`Could not read file ${filename}.`); | |
}); | |
} | |
function createNotes(filename, note) { | |
console.log(`Creating note ${filename}:`, "\n", note); | |
fs.writeFile(filename, note, error => { | |
if (error) { | |
console.error(`Could not create file ${filename}.`); | |
console.error(error); | |
} | |
}); | |
} | |
/* Bind server to a hostname and port */ | |
const port = 8080; | |
const host = "127.0.0.1"; | |
server.listen(port, host, () => { | |
console.log(`Server running at http://${host}:${port}/`); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment