can't make PUT PATCH and DELETE request in node.js/express? - node.js

My delete request is not working,what i am trying to achive is on click of a button i want to make a delete request?
the HTML5 spec only allows GET or POST, unfortunately so i cannot use data-method or method anything else
<a href="/posts/delete/" method="delete" type="submit" >delete</a>
handling request :
app.delete('/posts/delete',(req,res)=>{
res.send('delete');
})
but it is returning:
Cannot GET /posts/delete/
istead of
delete
Is there any way to do this without Libraries and Ajax Call?
When i make request with get it works fine can someone also explains
why we need delete request when it can just be handeled by get ?
Also patch and put ,looks similar to post request what's the difference with example please ?
for further clarification this is rest-api but this works only with postman when i am using in browser Cannot GET /posts/delete/ is rendering for all the requests

Related

Unable to get redirect response from Node server using res.writeHead() and .end()

I'm writing a server in Node.js. I've been using the send-data/json package to send responses, with much success. For example, at the end of my API call I use this code:
sendJson(res, res, {some: content})
This works great. However, I have a need to implement a URL redirect in one of my API endpoints. The code I am seeing everywhere to do this looks like this:
res.writeHead(302, {Location: 'http://myUrl.com'})
res.end()
However, this change causes my server to stop sending responses on this endpoint.
What am I missing?
Note: this is vanilla Node without Express.
Update: to clarify, based on contributions in the comments, this is the result of a GET request, so I expect that redirects should be enabled by default. I am still curious why no response is coming back from the server at all, regardless of whether it is an erroneous redirect attempt or a successful one.

Can i use AJAX request to update the whole page like reqular http requests?

If i can update the whole page with AJAX when and why should i use regular http methods?
Can AJAX requests replace normal http methods?
i am using nodeJS expressJS
Yes, you can update the whole page using AJAX. Send request from user end and get back an JSON array from server containing all the information you want.
If you want to load data from .txt file or .html, then
$.ajax({ url: 'http://website/some'
, type: 'GET'
, dataType: 'html'
})
.done(function(data) {
$('#container').html(data);
})
.fail(function() {
console.log("Something went wrong!");
});
Just wondering why you are looking at AJAX when you need a complete reload.
Yes, definitely you can. But You will not ask this question once you know the use of AJAX.
For example, you will never ever update the whole page if you are using AJAX. For example, at least Navigation will remain the same and the body part will change. This is the use of AJAX and many web applications load like this. They keep navigation the same and then update rest of the web page.
You will have this kind of application where you will update the body of the page. You can use REST API to fetch the data in the form of JSON and then load this data using styling and Javascript framework like Angular, React, Backbone, etc.

How to get post body with Koa-router-forward-request?

I have the koa-router-forward-request set up. I make an axios call to it and that call is forwarded onto an API. I can do get requests and retrieve the information. I can't get post requests working. I want to forward the post request body from the original axios call onto the API how do I do that?
I have const composeRequest = body;
and in the request I have composeBody: composeRequest as an attribute but that does not seem to be working.
This is super late but I think what you are looking for is maybe to 1. ensure you are using bodyParser() i.e. router.use(bodyParser()) and 2. when hitting the Koa route pull any params you pass via Axios by ctx.request.body, all params should be stored in there to pull out.

Error handling in express based on how a resource is called?

I am using express.
I am handling a Get request like below.
router.get('/user', user.getSessionInfo);
On client side I am using Angularjs.
I want to customize my error handling in nodejs based upon how a resource is being called, i.e. when /user is called via an $http call or directly from browser url so that I can return(if an error happens) either a json error obj or an error page(html).
I tried using req.xhr to decide whether the call is an ajax or not but I am getting false in both cases(not getting X-Requested-With header via $http.get)
So to achieve my functionality Can I depend upon
req.headers['accept'];
as from ajax call it will be
"application/json, text/plain, */*"
and when called from the browser url bar it will be
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8"
Is my way the best or there is a better way of handling it?
There is another way,use query param(dot extension) to decide resource type:
GET /api/user.json
GET /api/user.html
GET /api/user.xml
and it seems than this solution is more complicated to achieve in Express.
req.xhr was removed in angular. Read here why removed and how to get it back
https://github.com/angular/angular.js/commit/3a75b1124d062f64093a90b26630938558909e8d

Fetch post data after a request in NodeJS

i' m a bit new to Node, so question may be stupid...
I am sending a POST request to a website (through http.request) and I want to be able to use the invisible POST data I get along the response.
I hope this is achievable, and I think so since I am able to preview those data in Chrome debugger.
PS : I understand that we can use BodyParser to parse and get those while listening for a POST call server side, but I have found no example of how to use it coupled with an http.request.
Thanks !
If the body of the HTTP response contains JSON, then you need to parse it first in order to turn it from a string into a JavaScript object, like this:
var obj = JSON.parse(body);
console.log(obj.response.auth_token);
More info on various ways of sending a POST request to a server can be found here: How to make an HTTP POST request in node.js?
Edit : So we figured it out in the comments. The value I needed was in the form to begin with, as a hidden field. My error was to think it was generated afterward. So I'll just grab it first then login, so I can use it again for future POST requests on the website.

Resources