POST request to C++ Rest (casablanca) server - node.js

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.

Related

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://

How can I return server side error to the client's HTML page in Express/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"
...
}

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.

nodejs mobile development: how control navigation flow

I am a nodejs newbie and would like to understand the navigation flow when using nodejs to serve mobile applications.
Moible app
index.html
Show all users
Nodejs server snippit
var myData = {
"employees": [
{ "firstName":"John" , "lastName":"Doe" },
{ "firstName":"Anna" , "lastName":"Smith" },
{ "firstName":"Peter" , "lastName":"Jones" }
]
};
res.send(myData);
Question: how do I display this data on another page (users.html)? I've worked with nodejs where I can just render to a specific path and it picks the appropriate Jade file but not sure how to do it since the html / js files are on the phone and not the server.
If you know of an example application I can just look through that code and figure it out.
Thanks for your help.
First of all you need to understand that your node.js is executed on server side, and all it can do - response on requests and do some logic, that stays on the server.
Then there is .html and .js that is sent to your clients (browser), and it is rendered and executed on client-side. This execution and logic is very different, and is focused to provide user interactions and render all sorts of data.
So all you need is be able to 'ask' server for data (request) and then get response, validate it in browser, if it is valid, you can render it using JS.
In order to make your life easier, consider using jQuery.
AJAX - to make requests to server and get response with data.
express.js - web framework for node, helps with routes.
And just generally - go and try things, experiment and it is better to understand whole picture or specific details frist, before you making any decisions.

Node.js works with CouchDB and Backbone.js, How json is being served?

I am trying to build a test app for learning Node.js. I came from wordpress background and Apache has setup most of backend logics for me. But now, I have to build my own. I have a question about how to serve JSON files from server side to client side. What is the workflow -- Backbone.js handle all client side Data manipulation, send/save/get/fetch from couchDB, serve JSON object from NODE.js backend?
I am using Express Microframework for building HTTP server, installed the Cradle middleware for access CouchDB NoSQL database. I successfully posted the data from Client side HTML (Jade template engine) to the CouchDB Database/Document and able to retrieve those data back from Server through Cradle middleware. Things work out great. But it was all done by Backend.
I want to use Backbone.js for my client side Javascript. Backbone.js is looking for JSON object which send back from the HTTP server. Cradle Middleware is able to create JSON object but only send them directly to the Jade Template, I could use Jade syntax for loop to iterate over the data object but it still not meet what I want for Backbone.js handle all the data entry. I realize that I need to get JSON obj via ajax ( either a file generated by HTTP then send back to the client OR send straight object to the client ). Cradle Middleware could not do that.
I did more research on this questions. I tried CouchApp, it does what I need. Using the Backbone.js to handling all the data, send/save/fetch data from CouchDB database. But it is a running in CouchApp, it is not an Express Node.js workflow. ( Maybe I am wrong or just do not how it work )
I tried backbone-couchdb.js. I read through the Details and still do not know it is going to help me to get what I want. ( Maybe need more tutorial or code example ). I am still thinking that I need a Backbone CouchDB driver to connect those two and somehow serving them by NODE.js backend.
Is there anybody who could tell me about how JSON file is being served by Node.js, how backbone.js interact with data save/fetch/get from CouchDB? What is the best practice / workflow? Other good resources, code examples, useful tools?
Cradle Middleware is able to create JSON object but only send them directly to the Jade Template
This is incorrect. You can just send the json back without rendering a template.
function(req, res, next){
db.view('user/byUsername', { key: 'luke' }, function (err, doc) {
res.send(doc); // or res.json(doc);
});
}

Resources