Shyam Hande has Published 46 Articles

Understanding the npm - Node module system

Shyam Hande

Shyam Hande

Updated on 13-May-2020 12:11:09

199 Views

In our earlier example of getting user input and storing it a file has only one file. But in real life scenarios, we will have to create multiple file to keep code simple and easy to read.Let’s see how to use module system in node.jsWe have App.js −const http = ... Read More

How Node.js works in background - A brief analysis

Shyam Hande

Shyam Hande

Updated on 13-May-2020 12:05:48

408 Views

Node.js uses only one JavaScript execution thread.Question is − how Node.js process multiple http or any other requests and there can be question on performance, security as well?Node.js starts event loop on application start and handles callback functions using it.Node.js maintains a worker pool. Long running operations are transferred to ... Read More

Understanding blocking and unblocking of code execution in Node

Shyam Hande

Shyam Hande

Updated on 13-May-2020 12:04:29

596 Views

Now, we have a file writing function writeFileSync in fs module as shown below −const requestBody = []; req.on('data', (chunks)=>{    requestBody.push(chunks); }); return req.on('end', ()=>{    const parsedData = Buffer.concat(requestBody).toString();    const username = parsedData.split('=')[1];    fs.writeFileSync('username.txt', username);    //redirect res.statusCode=302;    res.setHeader('Location', '/');    return res.end(); });Sync means ... Read More

Understanding the Event driven code execution approach in Node

Shyam Hande

Shyam Hande

Updated on 13-May-2020 12:02:44

194 Views

In earlier example in App.js , we saw how to parse data from request using data and end event.Code snippet below shows the if block for that −if(url === '/username' && req.method === 'POST'){    const requestBody = [];    req.on('data', (chunks)=>{       requestBody.push(chunks);    });    req.on('end', ... Read More

Parsing request Body in Node

Shyam Hande

Shyam Hande

Updated on 13-May-2020 11:57:25

3K+ Views

Earlier in simple code example, we saw how to route a request and creating a file to input the test data.Now, we want to save the user entered input data into text file.How Node.js handles incoming request dataNode.js reads data in chunks means it uses streams to read data . ... Read More

Redirecting requests in Node.js

Shyam Hande

Shyam Hande

Updated on 13-May-2020 11:50:28

2K+ Views

Now we have an App.js file shown below, we want to redirect the user back to ‘/’ once user name is received by node server. We will store user name in a file named username.txtInitial App.js file −const http = require('http'); const server = http.createServer((req, res)=>{    const url = ... Read More

Routing requests in Node.js

Shyam Hande

Shyam Hande

Updated on 13-May-2020 11:40:43

1K+ Views

Routing http requests is important because we want to execute different business rules based on request url and the responses will be different for each routes.Earlier we saw, we can get the url by request.url in node. Simple example of user name input with routes is shown below −const http ... Read More

Sending response back from Node.js server to browser

Shyam Hande

Shyam Hande

Updated on 13-May-2020 11:35:41

1K+ Views

App.js −const http = require('http'); const server = http.createServer((req, res)=>{    console.log(req.url, req.method, req. headers); }); server.listen(3000);As shown in above example code, we have request and response parameter object as an argument in createServer method.Response (res) object will be used to send back data to client. It has many properties, ... Read More

Understanding the http requests in Node

Shyam Hande

Shyam Hande

Updated on 13-May-2020 11:31:28

393 Views

App.jsconst http = require('http'); const server = http.createServer((req, res)=>{    console.log(req); }); server.listen(3000);Run it with command: node App.jsWe can see what is inside a request object by opening a browser and navigating to localhost:3000There is lots of information which will get printed on console window. We will see some important ... Read More

Understanding the Node lifecycle and event loop in node.js

Shyam Hande

Shyam Hande

Updated on 13-May-2020 11:26:49

2K+ Views

Simple http server in node.js will register an event loop which will keep on listening for http requests.Execution of file containing code for creation of server will work as shown below −node App.js => Start executing script => Code parsing, Register events and functions => event loop=> keeps on running ... Read More

Advertisements