Skip to content

Commit

Permalink
example of blocking code
Browse files Browse the repository at this point in the history
  • Loading branch information
dasanupam1001 committed Aug 21, 2023
1 parent fb615e8 commit 9ab74f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 25 additions & 0 deletions 14-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// why sync approach for nested loops blocks the other codes from running

const http = require('http')

const server = http.createServer((req, res) => {
if (req.url === '/') {
res.end('Home Page')
}
else if (req.url === '/about') {
// BLOCKING CODE !!!!
for(let i = 0; i < 1000; i++){
for(let j = 0; j < 1000; j++){
console.log(i, j)
} // this will take much time and the server will be on reload
}
res.end('About Page')
}
else {
res.end('Error Page')
}
})

server.listen(5000, () => {
console.log('Server listening on port : 5000... ')
})
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
"description": "",
"main": "10,11-filesystem-module.js",
"scripts": {
"start": "node app.js",
"dev": "nodemon app.js"
"start": "nodemon app.js"
},
"keywords": [],
"author": "",
Expand Down

0 comments on commit 9ab74f4

Please sign in to comment.