Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Programming Articles - Page 1959 of 3363
437 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
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
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
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
985 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
603 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
429 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
158 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
223 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
529 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