How can I return server side error to the client's HTML page in Express/node js? - node.js

I am creating an web app I which the user requests to create an account. The request from client's page will reach the server and query the database. Now what is the proper way of displaying User already exists error in my registeration page. In short, how can I send server side errors to my clients in Node JS?

There are some options to get what you need. The approach that I would not follow will be using the http protocol and sending a 500 response for example.
One good approach would be creating a status and a message fields in your response.
You could use code 200 for the sucessful requests and a different code for the rest. You could have a dedicated code to inform the api user that the backend coud not insert the data.
Example response:
{
code: 789
message: "User could not be inserted"
...
}

Related

Render based on server's response

I am a new SPA developer in React, and I'm building a full-stack web application, [back-end and front-end], while planning, I stopped at a point that I couldn't find a convenient method for.
On the Login page, after the user enters his username and password, and hits submit, the form will send a POST request to the server, the server will verify the information provided, if the username and password were true, the server will ask the database to see if the user has activated the two steps verification, in case the user has activated it, the server will send a verification email to the user...
This is well-known, but the point is, how the server is going to tell the client (the React web app) that "hey react, you should render a box of "enter the code sent to your email" rather than rendering the user profile page because this user has two-steps verification enabled.
What is the convenient and the most logical way of writing this response from the server-side?
I was thinking of the following response:
// this is the response from the server after submitting the form (sending an AJAX POST request) HTTP code #200 code
response:{
UI: {
showEmailVerificationBox: true,
}
}
Is there a standardization for these kinds of responses? where the server wants to tell the client to do something in its UI?

Getting data from my database via my api(express) in react

I am making my first mern app and i want to populate the dashboard when someone logs in i was thinking that I should make a get request in my server and call my database in the get request and do the work from there but I heard somewhere that I shouldn't do this because its dangerous to call sensitive data in get request , so I am not sure that how I should get that data in my react app.
You can use a Post request.
I Recommend the Axios Library it is the most used HTTP Request library
https://www.npmjs.com/package/axios
then do
axios.post("https://url.de/request", {
data1: "hallo",
data2: "secret data"
})
in express the data is then in req.body.data1
EDIT: the Data is only secure when the url starts with https://

Express API response directly outputted in browser instead being handled by Nuxt.js

I have an express server that is connected to mongodb. I have a nuxt route for /account under pages directory. I also have a same route in the express server app.use('/account', account) which is handling the requests.
The database model has been set correctly and the response is sent by the server using return res.status(200).json(User) whenever the route is called upon using a get request. I'm also seeing the response being delivered to the browser.
But the problem is the response is being outputted directly in the browser as raw json. Nuxt is not being loaded to handle the response. I haven't setup any $axios data call in the component using asyncdata or fetch yet.
Here's the sample response that i see in the browser
{"email":"sample#example.com","account":"123445667777"}
I'm not sure what the issue is?

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.

POST request to C++ Rest (casablanca) server

I am new to full stack development and have few queries, I am working on a project (basically for self learning) in which I want to create a front end using node/express js and at backend want to use a mysql database. Connection between my website and database will be done through a rest server. This rest server will be implemented in Casablanca and will also have logic regarding how to handle database.
So my main concern is how could I handle post request in Casablanca rest server? Motive is to submit form at my site created using node/express js. Forward the value received through form (i.e. through node js rest client) towards Casablanca Rest server which will further update the database.
How could I handle such (post) request in casablanca and fetch values? I have tried few things to make this work and the latest that worked to some extent is mentioned below:
To support HTTP POST request below line is added in the code or listener is registered with POST method:
listener.support(methods::POST, handle_post);
And in handle_post method I am trying to extract the json through http_request::extract_json Method, as below:
void handle_post(http_request request)
{
try
{
json::value v = request.extract_json().get();
someFunction(v); //to iterate over JSON and update database
}
catch(http_exception const & e)
{
std::wcout << e.what() << std::endl;
}
}
After this when I sends a POST request (form Postman plugin of chrome), I am getting 500 internal error, even my someFunction is not getting called. Can someone please provide a clue of what exactly I am doing wrong here?
It could be possible that my whole implementation approach is wrong but it will really be very helpful if someone could provide me some pointers in the right direction.
Thanks in advance :)
You need to reply to the client with a status code
request.reply(status_codes::OK, U("Hello World!"));
just swap the hello world for the values you want to return from the server.

Resources