my app is making ajax calls against an api. I’m using axios to make my requests. The api sends the pagination info via headers.
and axios has a header property in its response. however , I don’t see any of the headers I want inside my code, even I logged the whole response object and my header keys weren’t there. Surprisingly when I check the response in my chrome dev too network tab. The pagination stuff are there.
this is header I get in my code
x-total-count: "500",
pragma: "no-cache",
content-type: "application/json;
charset=utf-8",
cache-control: "no-cache",
expires: "-1"
this is what I get in chrome dev tool
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://127.0.0.1:3000
Access-Control-Expose-Headers:X-Total-Count, Links
Cache-Control:no-cache
Connection:keep-alive
Content-Encoding:gzip
Content-Type:application/json; charset=utf-8
Date:Wed, 02 Nov 2016 15:10:57 GMT
ETag:W/"2606-KzmHaw5B5nHWkO2sfZlE0w"
Expires:-1
Link:<http://localhost:3001/articles?_sort=lastHourViews&_order=ASC&_page=1>; rel="first", <http://localhost:3001/articles?_sort=lastHourViews&_order=ASC&_page=2>; rel="next", <http://localhost:3001/articles?_sort=lastHourViews&_order=ASC&_page=50>; rel="last"
Pragma:no-cache
Transfer-Encoding:chunked
Vary:Origin, Accept-Encoding
X-Content-Type-Options:nosniff
X-Powered-By:Express
X-Total-Count:500
for example i have this json
[
{
shelveId: 0,
books: [{...},{...},{...}]
},
{
shelvId: 1,
books: [{...},{...}]
}
]
i would like to get a json as {shelveId, numBooks}
so in our case:
{
"0": 3,
"1": 2
}
how can i acheive that with the server using the lowdb functions and lodash i tried but no example of foreach or map, thanks!
"/test/:start/:end": "/pagination?_start=:start&_end=:end"
this doesn't work
res
.
isAuthenticated(req)
method, I want to check if the request had a certain header (auth token). I keep doing console.log(req)
to see where I can find the headers I send.. but to no avail - I can not for the life in me find headers in the request.
req.headers
doesn’t show the token that I do send in my req, actually those headers are all different from the ones I send.
req.headers
is the object of the headers sent from the client
const isAuthorized = (req) => {
return req.headers['AUTH_TOKEN'] ? true : false;
}
I have a nice solution using --middleware ;)
package.json:"startAPI": "json-server --watch server/mockedBackendAPI.json --routes server/mockedBackendRoutes.json --port 3004 --middlewares server/middleware.js"
middleware.js:module.exports = (req, res, next) => {
res.header('X-Hello', 'World')
next()
}