Routing from http POST to http GET in Express - node.js

I have some questions about routing using Node.js and Express.
I have a running application where you can add items in different ways (e.g. via 'URL'). Let´s describe that further:
I have to different routes:
/create: Choose adding option in a form with dropdown list (e.g. via 'url')
/create/validate: Form which shows the retrieved data
/create:
When pressing the "Add" button on my main page an GET request on this route will render a form where you can choose the inserting option.
In this form you can choose in a dropdown list one option (e.g. 'URL') and press the "Submit" button. The POST request collect the entered information which are available inside the req.body.
Before sending the entered data to my database I want to perform a validation. That means, the entered data is rendered in a validation form (which is shown to the user) on route /create/validate.
The problem here is, that I can´t send the entered data from /create POST request to the /create/validate GET request for the form.
Right now my /create POST request is redirecting to the /create/validate route and saving the url content in a local variable, which can be then accessed in my /create/validate GET request for further processing.
So summing up:
I want the entered data of the /create POST request available in the /create/validate GET request.
Is there any other way instead of saving the content to a local variable?

The best way is just keep a global variable somewhere outside your http handlers. That data will stick around, between request, on the server side, as long as the node process is running.
Otherwise an other solution is using localstorage for node.js
https://www.npmjs.com/package/node-localstorage

Related

How do I save form data using react-router-dom?

I have a route with a component that displays some form data. When I hit the submit button, I'll navigate to a different route that contains a receipt. If the user hits the back button, I want to remember what was entered in the form. How can I achieve this when using react-router-dom?
What you need is some type of data/state management for your react application.
Mobx is a good example for this. Search up how mobx works and how you can use this together with react.
With Mobx, you can store the user's form inputs in some type of class that you can decorate with mobx (for example DataStore class), which you can then read on your other page when you navigate there with router.

Creating form data via rest doesn't trigger autoresponder

Kentico 10. When I make a rest request to post data to the kentico form module I get a successful http 201 back and I can see the data created in the kentico interface. This particular form I am posting data to has an autoresponder set up.
I have a default smtp server set up and can successfully email out through it, which leads me to believe that the autoresponder on the form does not get triggered when data is created in the kentico form via rest. I never see the autoresponder attempt to send an email in either the email queue module or the event viewer (with the proper web.config key set).
Is that the intended behavior, to not trigger the autoresponder when forms module data is created via a rest request? If so, any idea how to get it to trigger?
Yes you are correct. Kentico REST is meant to perform simple CRUD operations. Essentially what you doing is the same as if you do it inside the admin i.e. go inside admin -> forms -> your form name -> recorded data and add new record. It doesn't trigger anything like sending email or Redirect to URL etc.
What you can do if you want to send email using form global events and on insert send email yourself. Here is the example of using global events

How does body-parser know if we sign up or log in?

I'm building my user authentication and the tutorials don't really get into details in 100%.
The examples always show how you get logged in if you submit data, assuming it's in the database already.
But on many HTML pages, you see sign in and log in button next to each other. How can I make body parser recognize the required function depending on which button is pressed? Both cases involve submitted data after all.
Thanks a lot!
PS. I'm using EJS view engine.
Body parser has nothing to do with log in or sign in.
It's purpose is to parse request's body by checking request header Content-type and using appropriate method to parse it. For example if content is json it will use JSON.parse and assign the result to req.body.
The buttons could be anchors with href set to /login or /signin so on your server you need login and signin routes to handle each of the actions

How can I create an authenticated scraper for Amazon product detail in Node.js?

I'm creating a script that grabs all the shipped items from Amazon and notifies me.
Authentication is needed to see the products though.
I've already tried sending a post request through "request" which returns an error because of the cookies and extra parameters needed.
It would be easy using cheerio afterwards to get the data if the authentication works.
Does anyone have any idea on how we can authenticate successfully?
The link from the email is: https://www.amazon.com/ap/signin/185-3199906-8918341?_encoding=UTF8&accountStatusPolicy=P1&openid.assoc_handle=usflex&openid.claimed_id=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.identity=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0%2Fidentifier_select&openid.mode=checkid_setup&openid.ns=http%3A%2F%2Fspecs.openid.net%2Fauth%2F2.0&openid.ns.pape=http%3A%2F%2Fspecs.openid.net%2Fextensions%2Fpape%2F1.0&openid.pape.max_auth_age=0&openid.return_to=https%3A%2F%2Fwww.amazon.com%2Fgp%2Fyour-account%2Forder-details%2F185-3199906-8918341%3Fie%3DUTF8%26eoid%3D1%253A1%253Arv%252FYwjiYmnOZY9MYltVnDyf2l6p5pMkMx9deoUeiiw%252FKpPrtZrWqs5l1GGQPVb%2520qaJqHXyCkPEpLZnmDZamKkVDWhtu3dKlW5Gx7Uvxtzs0xlPJ25vduijJrPpHt79P%2520RRZHopOtAyOP4s82VLoeeiDQgq%2520FCP540H%2520UYAV7goZQxB29WObWAVh8VveTwEeWenY3sTx8ZI9%252FBLM2BSqS3IUIURW8mzMnAB9t7wglUiAcoR%252FcUhSIx%25201eNV4MspVAp7fLkeANag72BxgmsjFfRhnsxfji1VhZXLawqFeK9SBnvbUfkNWUC%2520IXWh6VcuoStBG3x%2520ZUkzGHw1ORi4J%2520Hg%253D%253D%26orderID%3D105-6914722-5422613%26ref_%3DTE_simp_on_T1&pageId=webcs-yourorder&showRmrMe=1
You cannot guarantee any of the form input values of the sign in page. So you must also scrape the login form.
Here is the process:
In your server, make Request to the URL in your question
Using Cheerio parse the DOM and grab all of the form fields from "#ap_signin_form".
Add in your data (Username/Pass) then make a POST request to the form action "https://www.amazon.com/ap/signin" (This should also be scraped)
Hopefully that will get you past the login screen. You will need to ensure all future requests pass the cookies set from login.
Now this kind of thing is clearly against most TOS's so I would urge caution in doing this kind of thing often.

Hide querystring in url using MVC 5

Hi i have this application that needs a user to login.
Once the user is logged in, he is redirected to a page displaying documents for that user.
To display that information, I call the correct action on the controller and i pass my user-object. This object contains username and password.
When i look at my url it looks like :
http://localhost:53703/Documents?UserName=bart&UserId=10&Password=AllPhi%242015
Is there a way that I can hide those querystring-values (UserName=bart&UserId=10&Password=AllPhi%242015)
I can not object strongly enough to sidestepping the built in auth-mechanisms, but to answer the question: You cannot hide the query-string.
If you want to hide data when you are sending from a client you need to do a post request instead of a get, but the post-data is still visible in the request (in plain text)
But in this case it seems you want to pass data between actions, and then you want to use tempdata. Look here for reference: http://rachelappel.com/when-to-use-viewbag-viewdata-or-tempdata-in-asp.net-mvc-3-applications

Resources