Shyam Hande has Published 72 Answers

Styling html pages in Node.js

Shyam Hande
Published on 13-May-2020 13:36:53
In html files we can simply add style in head section −<html>    <head>       <style>          //add css code       </style>    </head> <body> </body> </html>We can add inline css styles as well in html directly.Generally css is separated from the html ... Read More

Serving html pages from node.js

Shyam Hande
Published on 13-May-2020 13:34:18
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() ... Read More

Filtering paths and creating html page in express.js

Shyam Hande
Published on 13-May-2020 13:31:42
We added express router to handle routes. One single router file handles multiple routes.Adding a path for router in App.js −const 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('/test', route); app.use((req, res, next)=>{    res.status(404).send('<h1> Page not ... Read More

Adding 404 page in express

Shyam Hande
Published on 13-May-2020 13:28:57
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 = ... Read More

How to use express router

Shyam Hande
Published on 13-May-2020 13:26:34
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 ... Read More

Using post request in middleware in express

Shyam Hande
Published on 13-May-2020 13:24:12
We used use() function to execute middleware’s . Example we used below will be executed for both http GET and POST method −const http = require('http'); const express = require('express'); const bodyParser = require('body-parser'); const app = express(); app.use(bodyParser.urlencoded({extended: false})); app.use('/', (req, res, next)=>{    next(); }); app.use('/add-username', (req, res, ... Read More

Parsing incoming requests in express.js

Shyam Hande
Published on 13-May-2020 13:22:03
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('<form action="/post-username" method="POST"> <input type="text" name="username"> <button    type="submit"> Send </button> </form>'); });For parsing the http request, we requires a third party library body-parser: It’s a production required dependencynpm install ... Read More

Handling different routes in express.js

Shyam Hande
Published on 13-May-2020 13:19:50
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('/', ... Read More

Adding middleware in Express in Node.js

Shyam Hande
Published on 13-May-2020 12:41:03
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)-> ... Read More

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

Shyam Hande
Published on 13-May-2020 12:38:06
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 ... Read More
1 2 3 4 5 6 7 8 Next
Advertisements