Found 26504 Articles for Server Side Programming

Get positive elements from given list of lists in Python

Pradeep Elance
Updated on 13-May-2020 14:00:46

492 Views

Lists can be nested, means the elements of a list are themselves lists. In this article we will see how to find out only the positive numbers from a list of lists. In the result a new list will contain nested lists containing positive numbers.With for inHere we simply apply the mathematical operator to check for the value of elements in a list using a for loop. If the value is positive we capture it as a list and Outer for loop stores as a final list of lists.Example Live DemolistA = [[-9, -1, 3], [11, -8, -4, 434, 0]] ... Read More

Styling html pages in Node.js

Shyam Hande
Updated on 13-May-2020 13:37:19

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
Updated on 13-May-2020 13:34:35

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

Filtering paths and creating html page in express.js

Shyam Hande
Updated on 13-May-2020 13:32:50

402 Views

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(' Page not found '); }); const server = http.createServer(app); server.listen(3000);In router middleware we used path −/p>app.use('/test', route);Router will handle all paths starting with /test e.g. /test/add-usernameWe have to change action in form in routes.js file −router.get('/add-username', (req, res, next)=>{    res.send('     Send '); });Routes.js file −const express ... Read More

Adding 404 page in express

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

993 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
Updated on 13-May-2020 13:27:17

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

Using post request in middleware in express

Shyam Hande
Updated on 13-May-2020 13:25:04

1K+ Views

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, next)=>{    res.send(' Send '); }); app.use('/post-username', (req, res, next)=>{    console.log('data: ', req.body.username);    res.redirect('/'); }); app.use('/', (req, res, next)=>{    res.send(' first midleware: Hello Tutorials Point '); }); const server = http.createServer(app); server.listen(3000);How to restrict middleware only for GET or only for POST http method ... Read More

Parsing incoming requests in express.js

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

951 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

576 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

400 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

Advertisements