-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasync_patterns.js
More file actions
41 lines (35 loc) · 982 Bytes
/
Copy pathasync_patterns.js
File metadata and controls
41 lines (35 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//Async patterns - Blocking code
// We should always code asynchronously so that it does not block code and rather gets
// offloaded
const https = require("https");
const server = https.createServer((request, response) => {
if(request.url === "/") {
response.end("Home Page");
}
else if(request.url === "/login") {
//Synchronous blocking code
for (let i = 0; i <1000; i++){
for (let j = 0; j < 1000; j++){
console.log(`${i} - ${j}`);
}
}
response.end("Login Page");
}
else {
response.end("Page not found");
}
})
server.listen(5000, () => {
console.log("Server is listening on port 5000...")
});
// Async patterns - Setup
const {readFile} = require("fs");
readFile("fileSystemContent/first.text", "utf8", (err, data) => {
if (err) {
console.error(err);
}
else {
console.log(data);
}
})
//This is async pattern and is non-blocking