Node.js Articles

Page 20 of 22

Async Copy in fs-extra - NodeJS

Mayank Agarwal
Mayank Agarwal
Updated on 27-Apr-2021 888 Views

Introduction to Async copyThis method copies files or directories from one location to another location. The directory can have sub-directories and files.Syntaxcopy(src, dest[, options][, callback])Parameterssrc – This is a string paramter which will hold the source location of the file or directory that needs to be copies. If the location is a directory, it will copy everything inside of the directory instead of whole directory.dest – This will hold the destination location where the files/directories will be copies. If src is a files, dest cannot be a directory.options −overwrite – If set to true, existing files or directories will be ...

Read More

Styling html pages in Node.js

Shyam Hande
Shyam Hande
Updated on 13-May-2020 7K+ Views

In html files we can simply add style in head section −                    //add css code           We can add inline css styles as well in html directly.Generally css is separated from the html code. Third option add css is to include a css file .How to serve static files in Node.js?Generally css files are added with below tag −     Express js provides a middleware to seve static file. This middleware gives read access to given folder.app.use(express.static(path.join(__dirname, ‘public’)));path: its our core module ...

Read More

Serving html pages from node.js

Shyam Hande
Shyam Hande
Updated on 13-May-2020 2K+ Views

So far we sent html code directly from the send(0 function in response object. For sending larger code, we definitely require to have a separate file for html code.sendFile() function−Response object gives a sendFile() function to return a html file to client.How to provide path to html file in sendFile() ?We import the path core module of node.js.const path = require(‘path’);path has a join function . __dirname is a global variable which holds the actual path to project main folder.path.join(__dirname, ‘views’, ‘add-user.html’); This will refer to the actual file location of add-user html code.App.jsconst http = require('http'); const express = ...

Read More

Adding 404 page in express

Shyam Hande
Shyam Hande
Updated on 13-May-2020 1K+ Views

Now we have a App.js and route.js for handling routes. For any other http requests for which we have not added any request handling will results into an error page. Example for url ‘test’ −App.jsconst http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const route = require('./routes'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use(route); const server = http.createServer(app); server.listen(3000);Showing meaningful error message on incorrect url’s−We can add a all catch middleware for incorrect url at the end of all middleware’s in App.js −const http = require('http'); const express = require('express'); const ...

Read More

How to use express router

Shyam Hande
Shyam Hande
Updated on 13-May-2020 2K+ Views

In earlier examples, we wrote all routing code in a single file App.js. But in real world scenarios, we have to split the code into multiple files.We can create separate files and import them but express gives a router mechanism which is easy to use.Create a separate file called route.js (name can be anything)Create router using express −const express = require('express'); const router = express.Router();exporting router −module.exports = router;Adding routing functions −router.get('/add-username', (req, res, next)=>{    res.send(' Send '); });  router.post('/post-username', (req, res, next)=>{    console.log('data: ', req.body.username);    res.redirect('/'); });Similar to functions we used in App.js ...

Read More

Parsing incoming requests in express.js

Shyam Hande
Shyam Hande
Updated on 13-May-2020 1K+ 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
Shyam Hande
Updated on 13-May-2020 639 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

Understanding the different error types and handling in Node.js

Shyam Hande
Shyam Hande
Updated on 13-May-2020 551 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
Shyam Hande
Updated on 13-May-2020 838 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 - Node module system

Shyam Hande
Shyam Hande
Updated on 13-May-2020 205 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
Showing 191–200 of 212 articles
Advertisements