Server Side Programming Articles - Page 1788 of 2650

Understanding the different error types and handling in Node.js

Shyam Hande
Updated on 13-May-2020 12:33:34

516 Views

Error types are −Syntax errorRuntime errorLogical errorSyntax error −These are easy to find as most of the development tools like visual code studio shows red lines whenever there is a syntax error. The suggestion for resolution may be not be correct in tools but it gives an idea of what went wrong in a specific area of code.On running an app, console terminal will shows the errors. Console log can point to exact line where error occurred.More common syntax error are like missing closing bracket for a block , it will need to identify correct blocks .Runtime error −Example − ... Read More

How to install third party packages using npm

Shyam Hande
Updated on 13-May-2020 12:30:31

749 Views

Now, so far we saw how to create a node project with npm init command and also adding scripts to run application.Why third party libraries are requiredWe used core modules of node.js like http, fs etc which are available by default with node.js but working only with these core modules does not simplify our work. To add more useful functionality and simpler code we requires to install third party libraries like express, body-parser etc.We get the third party libraries from cloud stored npm repository. Installation is done using npm install command.NodemonWe are running our App.js file using npm start command. ... Read More

Understanding the npm scripts in node.js

Shyam Hande
Updated on 13-May-2020 12:15:36

360 Views

So far we are running our App.js using following command −Node App.jsWe can use npm scripts to run or debug our app.How to start a node projectCommand is − npm initAbove command will initiate a project, it will ask few questions about project name and starting file name etc.As we have App.js file already give App.js file as starting entry file name. npm init command will create a package.json file from where dependencies for project can be added/updated/removed.Package.json file looks like this, Its in json file format as per file extension suggests −{     "name": "dev",    "version": "1.0.0", ... Read More

Understanding the npm - Node module system

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

172 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 = require('http'); const fs = require('fs'); const server = http.createServer((req, res)=>{    const url = req.url;    if(url === '/'){       res.write('');       res.write(' Hello TutorialsPoint ');       res.write('       Submit ');       res.write('');     ... Read More

How Node.js works in background - A brief analysis

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

377 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 this worker pool and event pool only handles responses from this worker pool on completion of tasks.Worker pool works with operating system to do the heavy lifting of work and managing scheduling of tasks.Worker pool once finished task responds to event loop using callback functions.Event loop maintains the order of ... Read More

Understanding blocking and unblocking of code execution in Node

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

557 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 synchronized. It’s a blocking code example. Once file write is completed then only code execution for rest of the file starts. Above code is simpler but if we have a large file handling operation it will result into slow performance of app.This way of code execution will slow down the ... Read More

Understanding the Event driven code execution approach in Node

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

177 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', ()=>{       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(); }In above code block, we have two events (data and end) registered if path matches to ‘/username’ and ... Read More

Parsing request Body in Node

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 . Once node completes reading request data, we can proceed to use it for our purpose.First read data in chunks const requestBody = []; req.on(‘data’, (chunks)=>{    requestBody.push(chunks); });We have registred an event called ‘data’ on incoming http request. This event will keep on streaming data and pushes to requestBody const ... Read More

Redirecting requests in Node.js

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 = req.url;    if(url === '/'){       res.write('');       res.write(' Hello TutorialsPoint ');       res.write('       Submit ');       res.write('');       return res.end();    }    res.write('');    res.write(' Hello TutorialsPoint '); ... Read More

Routing requests in Node.js

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 = require('http'); const server = http.createServer((req, res)=>{    const url = req.url;    if(url === '/'){       res.write('');       res.write(' Hello TutorialsPoint ');       res.write('             Submit ');       res.write('');     ... Read More

Advertisements