Found 285 Articles for Node.js

Parsing incoming requests in express.js

Shyam Hande
Updated on 13-May-2020 13:22:29

705 Views

To receive some data in http request , lets add a form on url path ‘/add-username’:app.use('/add-username', (req, res, next)=>{    res.send(' Send '); });For parsing the http request, we requires a third party library body-parser: It’s a production required dependencynpm install –save body-parserexpress js provides middleware use function to include a body parser before adding middleware.const http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({extended: false}));the use(0 function shown above uses next() function by default so http request gets passed to next middleware without any trouble.The above ... Read More

Handling different routes in express.js

Shyam Hande
Updated on 13-May-2020 13:20:49

251 Views

For handling different routes, use() function is used. use() function has multiple overloaded version, one of version also takes url path as a argument. Based on the url path, the requests will be filtered out for respective middleware.const http = require('http'); const express = require('express'); const app = express(); app.use('/', (req, res, next)=>{    console.log('first middleware');    res.send(' first midleware:    Hello Tutorials Point '); }); const server = http.createServer(app); server.listen(3000);in above example we used ‘/’ as url path, it’s a default.Now, as every route starts with ‘/’, the above middleware executes for every http request. It works for ‘/’ ... Read More

Adding middleware in Express in Node.js

Shyam Hande
Updated on 13-May-2020 12:42:00

271 Views

Each request in app goes through multiple middleware’s in express. If one of the middleware returns the response it ends there. If any middleware wants to pass the request to next middleware, it uses next() function call at the end of its function call.Http Request -> Middleware (req, resp, next)-> Middleware(req, res, next)-> Http Response ( res.send() ).const http = require('http'); const express = require('express'); const app = express(); app.use((req, res, next)=>{    console.log('first middleware'); }); const server = http.createServer(app); server.listen(3000);Middleware’s are added using use function as shown above. Use() function receives three arguments basically request, response and next() function.The ... Read More

What is express.js and installing it in Node.js?

Shyam Hande
Updated on 13-May-2020 12:38:42

64 Views

Why requires express.js?Writing core node.js code to fetch request data and parsing it is complex enough. As we saw in previous posts, we wrote data and end event to get the simple request data.Express makes the process simpler. It helps developers to focus more on writing business logic instead of node’s internal complexity.Express.js does the heavy lifting of node’s internal workings. There are some other alternatives to express.js are also available e.g. Adonis.js, Sails.js etc.Installing express.jsWhy –save and not –save-dev for express?Express is a major runtime required library so it’s a dependency and not just dev dependency . That’s why ... Read More

Understanding the use of debugger in Node.js

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

99 Views

In earlier example, we used debugger in vs code to check any logical error. In this article we will look into how to use debug console and restarting debugger automatically on changes.In debug console, we can also type an expression and evaluate its result beforehand. This is very useful in finding logical errors.In left menu’s we can also see variables and expressions in watch like −In variables section, developer can change the values of variable by double clicking on its values and edit. This will directly change in app runtime and will take effect of values.Example of such a one ... Read More

Understanding the different error types and handling in Node.js

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

281 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

534 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

262 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

93 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

255 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

Advertisements