POST calls not reaching Controller, failing with 500 - node.js

In my KrakenJS Node App., I am Trying to test a controller functionality which ultimately does a POST call to a web server.
I am adding a new route in routes/index.js like below so that i can directly test it :
router.post('/testPost', testController.testPostCall);
In this case when i try to call a POST on this URL via Rest Clients ; the call does not even reach the controller & fails with 500 :
(t) URL TestNodeAPP_testPost
(E) LOG INFO
(A) RENDER errors_500 53ms
(T) URL TestNodeAPP_testPost 3391ms
**POST /TestNodeAPP/testPost 500** 3445.722 ms - 2738
But if i change the router method to get like below, the call reaches the controller :
router.get('/testPost', testController.testPostCall);
What needs to be done to make it work for POST call?

Related

Waiting Response from Axios From Express js

i got a issue so i wrote this in my code this is the controller :
on my service i call like sdk to hit external API and i need to get the result of the response from that external API
on my sdk i use Axios like this :
i tried to execute but the response is always return when the status on my API already returned
My question is How to waiting the response from external API like my case ?

Express middleware know if incoming will be a 404

(i know there are a few official tools for SSR but we decided to roll our own for various reasons that are not important for this question)
We have a prototype of a server-side renderer running in nodejs for vue with Puppeteer. It works very well and now I would like to cache routes we want to cache. The cache invalidation is simple.
We intend to use Redis to hold the cache objects.
Proposed flow of the server:
Incoming request hits the express app
An express request middleware 1st checks if the route should be cached, if not return index.html. END else continue.
The same middleware checks if there is a Redis cache object based on incoming request object data, if yes return cache & END else continue.
Express router matches the request to a route, passes the request to the respective domain layer, creates the cache then returns it. END else next.
No express route found, handle 404 & END.
The question I have relates to proposed flow step 2
To know if the request should be cached is based on 2 parameters:
does the request cookie contain a valid token
is the request path one which is also defined in the express routes
The express routes currently look as follows, but is liable to evolve:
GET /channel/:name
GET /channel/:name/:tag
GET /item/:id
So, how in step 2 can we check if the incoming route is a route that would match the express router?
As said in first comment, express already provde a way to do this :
app.get('/channel/:name/:tag', YOUR_MIDDLEWARE, (req, res) => {
// Your Handler
})

How do I debug Axios frontend POST (React) to backend express POST? Console shows request as POST and status 200

Browser: Safari
Texteditor: VS CODE
Frontend framework: Reactjs
Backend setup: Express.js
Problem: I have an Axios post request on the front end after an onClick call event is triggered. I am unable to debug the data being sent in the body and know why it isn't getting over to the backend.
The screenshots show the code:(not the breakpoints in red. I am not sure where a good place to use debugger;
This is the code for the onClick event
This is the Backend code running on express port 8000
This is a screenshot of safari and the form being submitted. In the console is the response. As you can see it shows as post request type and status 200. None of that data gets over to the backend SQL database.
Note in the screen above that the object is rendered several times. That is because to get this code to show the results in the console I changed this function.
to this:(though according to react the above is the correct way and will only fire the function once when onclick event is triggered. The below approach does not require an onclick event.
Below is the data showed when setting a breakpoint on all requests. Note that it appears to be a GET request when it should be a POST
Note this is a POST REQUEST. This only happens when the onclick function is changed.
Any help on this matter would be greatly appreciated. I know I am close to figuring this out, however, I am still new to React and CRUD works with it.
So your backend isn't handling the request being made from the front-end. if you want to see the data, try doing this in your server. You need to handle the request your front-end is making before sending a response. The below code just destructures the request body so you don't have to write many lines of code. Once you click on the submit button, you should see that your front-end input is popping up on your back end console. You are currently trying to send an unhandled request. If you want to let's say send something to the front-end based on the request, you would do something like
res.json({nameOfThing: valueOfTheThing"}) AFTER HANDLING THE REQUEST.
app.post("/", (req, res)=>{
const {email, name, question} = req.body
console.log(`Your Email is ${email} and your name is ${name} and your ${question}`)
})
The data should appear in your console. I have actually written a blog on how to do stuff like this. Feel free to check it out. https://dev.to/gbudjeakp/how-to-connect-your-node-express-backend-with-your-react-front-end-post-610

Calling the correct get route in Axios/Node

I'm learning about Axios, Mongoose and Express and have come across the following issue. I am working on one schema that needs to have 2 different get requests accessible for when i need them. problem is that when i call the route with attending it still executes the route with info. How can i target the attending route on the backend correctly?
FRONT END
//finds a users info using findOne
axios.get("/api/user/" + info); //info is a variable
//finds a users info
axios.get("/api/user" + attending); //attending is a variable
ROUTES FILE ON BACKEND
router
.route("/:info")
.get(UserController.findOne);
router
.route("/:attending")
.get(UserController.findOneAndUpdate);
I have also tried changing the routes like below but still it hits the info route and not the attending route .
FRONT END
axios.get("/api/wmUser/getAttending" + eventCode);
ROUTE ON BACKEND
router
.route("/:info")
.get(UserController.findOne);
router
.route("/getAttending/:attending")
.get(UserController.findOneAndUpdate);
You are sending an axios GET request which will target
router
.route("/:info")
.get(UserController.findOne);
If you want to hit the PUT route on the backend, you need to do a axios.put request on the frontend.
Both your axios get requests from the frontend will hit the get on the backend with different params (namely info and attending).

204 error code then 500 error code responses

So I have an application which needs to send data to the API which is created by our team leader using NodeJS with Express.js.
On my end I have laravel application which using VueJS for the UI. Inside the Vue JS component. I am using axios to request to the API.
axios.post('https://clearkey-api.mybluemix.net/sendcampaign', request)
.then(function(response) {
//console.log(response);
})
However, it returns 204 which means according to this https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.
204 No Content
The server has fulfilled the request but does not need to return an
entity-body, and might want to return updated metainformation. The
response MAY include new or updated metainformation in the form of
entity-headers, which if present SHOULD be associated with the
requested variant.
If the client is a user agent, it SHOULD NOT change its document view
from that which caused the request to be sent. This response is
primarily intended to allow input for actions to take place without
causing a change to the user agent's active document view, although
any new or updated metainformation SHOULD be applied to the document
currently in the user agent's active view.
The 204 response MUST NOT include a message-body, and thus is always
terminated by the first empty line after the header fields.
Then next it returns 500 Internal Server Error. So in my opinion it returns this error because there is no content to be returned from the server?
Can you tell me other possible problems why it return that response?
Check if the "HTTP method" of the 204 is OPTIONS and if the method of the 500 is POST.
If both are like that, then you are seeing first a CORS pre-flight request (the OPTIONS that returns 204) and then the actual request (the POST that returns 500).
The CORS pre-flight request is a special HTTP message your browser sends to the server when the webpage and the backend are hosted at different addresses. For example, if your website is hosted at http://localhost but the backend you are trying to access is hosted at https://clearkey-api.mybluemix.net.
The reason of the 204 just means your backend endpoint is correctly setup to handle requests for /sendcampaign (you can ignore it). The reason of the 500 is because of some exception in the implementation of the function that handles that endpoint.

Resources