Error handling in express based on how a resource is called? - node.js

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

Related

MongoDB Atlas Function inconsistent behaviour between HTTPS call and debug run

Using postman to make a https request returns an error, whilst the function runs in the debugger.
This is best explained with screen grabs of the issue
Runs in the mongodb function editor: working function editor
Fails by postman request (http endpoint set to respond with result):
postman request error
Additionally .toArray() method causes this failure, which is annoyingly nondescript!
Solved: as the flexible sync queryable field was in the users custom data, this must be made available to the function with a toggle in the http endpoint settings

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't make PUT PATCH and DELETE request in node.js/express?

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

Setting Error Pages for a specific path

I am using ECS for a webapp, and i setup an Error Pages definitions for codes 404 and 502. My current setup returns a static maintenance.html file (hosted on S3) and returns a code of 200. I have to return 200, otherwise Outlook (which hosts my app) will not render the returned html, and show an ugly error page instead.
The problem I'm having with it, is that a later API call from my clientside app to my server, might also return 404. With the current setup, CloudFront intercepts this reply, and returns the maintenance.html with code 200. So now my app doesn't know anything is wrong, and later fails on parsing the reply.
Is there a way to define the Error Pages to only handle specific requests? For my usage, I'd like them to only handle calls to my /static/index.html files. If some other file/API call is actually missing from the server, I'd like my client to get the 404, so it can handle it properly.
So to answer your question according to the documentation I read there is currently no way to do this from cloudfront Error Pages Config.
However, We had a similar problem, but in our case API calls were returning a 500 Error with a s3 custom error html instead of the actual server error that I expected only for API calls.
What we ended up doing in this case was a handler in the back end for 500 errors that would return a response code that we were not using in the cloudfront Error Pages if the request Url had the pattern of API calls (we used http Error Code 406) and then the handler will reply with the 406 response code and the whole error instead of the 500 with the s3 response html only when failed over the api url pattern. I know this does not solve the problem the way you would have like to, but probably you may find it helpful until cloudfront allows a custom error response based in a Path Pattern.

Upload file on Express.js app

I'm developing a RESTful API for a mobile client application with the combination of Node.js, Express.js and Mongodb.
Now I'm trying to handle the upload of the user profile image and I've found a module called "multer" (that is the one ecommended by express.js team itself) that allow the express.app to handle multipart/form-data requests.
Now I need to test the app and, moreover, the upload function but I'm not able to simulate a http-form request (via postman chrome plugin).
Multer returns this error:
[Error: Multipart: Boundary not found]
In fact, comparing an http-form request (which works) with a custom http request, the second one has not the Boundary header property.
What Boundary property is?
If you are using Postman, you can try removing the Header: "Content-type": "multipart/form-data". I removed it and now it works.
Boundary in a multipart form indicates some delimiter string separating text and binary data. You can do this in postman but it sounds like you aren't sending both file and text so postman maybe defaults to a regular form. do you see something like:
If you click preview in postman you can see the boundary in the Content-type header and in the body.
solutions:
1) don't specify the content-type at client
2) use the naming convention(imageUpload) in upload.single('imageUpload') same as field name

Resources