Pass values on validation - node.js

Till now when I used to validate an input form in POST router in nodejs , I would use res.redirect. But this causes all the values from the form to clear.
Is it a good practice if I use again in POST router, res.render and pass the values like that to the inputs?
I ask on the long run.

Related

Node /Express, How to configure bodyParser to use raw on certain routes only

I'm not quite sure if I am approaching this correctly, but my problem is, that I receive usually json data at my API endpoint, then I am creating a model instance from the json req.body data...and I am using MEAN stack..
The problem that I now have is, that one of the fields of my model is of mongoose-long datatype with 18 positions. However, the bodyParser parses the number and rounds the las 3 positions of the number. I use it for the _id field, so I am getting duplicate key errors.
I construct and send the data manually via Python for testing, and I use _ast_lib_eval(document) to strip all strings quotes from my constructed json document. Therefore, somehow the json bodyParser is getting the body and takes the number but cuts off (or rounds) the number fields because I guess, it does not support such long numbers natively...I could use another bodyParser for certain routes maybe... but I'm not sure how to configure that.
What is the best approach now? Do I have to use another parser and therefore send my data as plain text or raw for all api endpoints? That would mean I have to redo everything so that it fits the parser, right? The usual format is json....
or can I somehow get the "raw" data from the req.body before it is getting parsed from the bodyparser? That would be the best way to manually get the number out right.. but I don't know if there's a method to do that...
Based on specification you are able to specific custom bodyparser on all endpoints
app.post('/some/special/route', bodyParser.json({verify:function(req,res,buf){req.rawBody=buf}}))
app.use(bodyParser.json())
Issue on gh

Slim 3: best way to pass variables (that depends on request) to layout?

I need to pass variable to a Twig layout about user country and language.
These datas are determined from the route, so I can't get them before routing is done.
I could set them from the route callbacks but code would be repeated in each route.
So I added a middleware to routes. It reads request arguments and sets variables (through $twig->getEnvironment()->addGlobal(...)).
I am not fully convinced, is there a better way to achieve this?
thanks

How to get user input in Yesod without using Yesod Form

In my page I have a search field(text area).
Yesod Form is quite complicated to use, so I figure I'll just use Javascript to get the value typed by the user. But then how my Haskell program gets that value?
You need to make a HTTP request to the server and pass it data.
In case of GET request, you just declare handler parameters in config/routes and then get them as getYourHandlerR arguments. In case of POST, you can use requireJsonBody to get data as JSON or getPostParams.

Are this two routes same in nodejs?

Routes in Express:
/search/:company-name
/search/:category-name
I can see that first one is fired for both requests so they are same, but is there a way to solve this without involving for example:
/search/company/:company-name
/search/category/:category-name
Yes, they are the same.
The router just see a route that starts with search/ and ends with a wildcard. The only thing that change is the name you give to that wildcard, which doesn't matter for the router, it's still the same URL.
You can solve this by either changing the route, or you can parse the route argument (the wildcard) and do something different depending on its value.
You could use a query instead of a param.
Your urls would be:
/search?company=company-name
/search?category=category-name
Your route is /search and you use req.query instead of req.params.
It's either that,
or your solution of changing the route,
or somehow parsing the parameter to decide whether it's a company or a category
or changing your route to a post and using key-value pairs in the post body

How can the submitted values in a POST message be accessed in Rust/Iron framework?

I am using a form which uses the POST method to submit the input. Is it possible to access the input like accessing a dictionary? I think there must be a better way than parsing the request body.
You can use the params crate for this.
With this crate, you can get the POST parameters with:
let params = req.get_ref::<Params>();
(See the example for a complete code.)

Resources