Using Method override in NodeJs

Using Method override in NodeJs

Declaring a method in sub class which is already present in parent class is known as method overriding. In nodejs methodOverride() Lets you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.

The methodOverride middle-ware is for requests from clients that only natively support simple verbs like GET and POST. So in those cases you could specify a special query field (or a hidden form field for example) that indicates the real verb to use instead of what was originally sent. That way your back-end .put()/.delete()/.patch()/etc. routes don't have to change and will still work and you can accept requests from all kinds of clients.

Following four HTTP methods are commonly used in REST. • GET - This is to provide a read-only access to resource. • PUT - This is to create a new resource • DELETE - This is to remove a resource • POST - This is to update a existing resource or create a new resource.

When we try to use PUT and DELETE in HTML, we have a problem. Browsers do support PUT and DELETE but it only by using request via AJAX. so we use methodOverride and this is a Node.js module available through the npm registry. Installation is done using the npm install command

$ npm install method-override Screenshot from 2020-08-12 13-13-27.png

Next is require methodOverride amo.png

Override using a header

umo.png

Logic using "put" method to override post method.

put.png

Call with query override using HTML :

afrm.png