Get request with query string in express - json

I want to be able to complete a get request on a local host port. The full url should be "localhost:3000/data/mydb.json?get=somefield" where mydb.json can be any filename and somefield is a key. In my app.js file my get request looks like this:
app.get('/data/:filename?get=somefield', routes.getdatabase);
regardless of what i put in the routes file it is unable to complete a get request on any sort of url matching this pattern. To debug i removed everything after the question mark (e.g. app.get('/data/:filename, routes.getdatabase);) and any filename works. Is there some module i need to require in order to use this query string syntax or is there anything else wrong with this get request?

Query strings are not a part of the route path.
Given your second example:
app.get('/data/:filename', routes.getdatabase);
You should be able to use req.query.get for your query param.
routes.js
exports.getdatabase = function(req, res, next) {
console.log(req.params.filename);
console.log(req.query.get);
// Do some stuff
return res.send(someResponse);
}
Console (URI: /data/someFile?get=cats):
someFile
cats

Related

how to insert query and save json data into sql server using nodejs

I am trying to insert query into SQL Server using window authentication with nodejs. I have done with get request of select query. but now am trying the post request with insert query. But I can't pass my req.body.address into the following query. the address data have json value.
here my code
here my request data,
the sql table row,
That is the error,
Your mysql library is probably, by default, applying a standard string conversion to req.body.address. When you do this to a javascript object you obtain [object Object]:
req.body.address.toString() // "[object Object]"
Objects needs to be converted to string using JSON.stringify():
"user_address": JSON.stringify(req.body.address)
You need to save as string in user_address field.
eg:
If you want to save as address like this:
user_address: `${req.body.address.street},${req.body.address.district},${req.body.address.city}`
Or
user_address: JSON.stringify(req.body.address)
whenever you want to show the address you need to do JSON.parse(user_address).
In nutshell, value must be a single value.

MySQL XDEV Nodejs Driver not returning empty resultset

i'm calling a stored procedure mysql using xdev nodejs driver. The stored procedure returns multiple resultset. When trying nextresultset, i'm getting only the non-empty resultset. So this messes with the resultant data index.
As far as I can understand, the problem is that nextResult() returns false when there are no items in the following result set.
This is an issue in a way that it breaks the following iterator pattern
const res = await session.sql('call proc_with_multiple_and_some_empty_result_sets()').execute()
do {
// do something
} while (res.nextResult())
and does not allow to easily consume the entire result set.
There are workarounds for this, such as using the toArray() method and iterate over the plain data:
res.toArray().forEach(item => {
// do something
})
However, this is a bug, and maybe you can submit a report on https://bugs.mysql.com/ under the Connector for Node.js category.
Disclaimer: I'm the lead dev of the MySQL X DevAPI Connector for Node.js

Node.js: How to read parameters from an axios get request?

So I have an axios request along the lines of this in my ReactJS project:
axios.get(URL, {params: {id: id}}
This would pretty much pass a get request to my backend with an URL like so:
URL?id=id
I also have a Node.js Lambda function that runs a mysql query like so:
connection.query("SELECT * FROM database.USER WHERE ID = '"+event.id+"'", function (error, results, fields))
So I know this does not work because I'm passing in a params through a URL, which does not work with event
I am trying to make my Node.js function read the id from my URL params, but I do not seem to know the syntax to do that.
Does anyone know how to get the params from a Node.js function? I've tried stuff like params.id, #id, :id, params[0].id, but none of them seem to work.
First of all you are exposing your backend to SQL Injection. You should NEVER concatenate your sql queries with values from the outside that you don't control. Please read about SQL injection: https://www.w3schools.com/sql/sql_injection.asp
You must use mysql2 library and prepared statements SEE: (https://github.com/sidorares/node-mysql2/blob/master/documentation/Prepared-Statements.md)
Since you are using AWS lambda you read values from the query using:
const id = event.queryStringParameters ? event.queryStringParameters.id : null;

AES_DECRYPT Getting the value from a blob in typescript

Coding an Angular 5/typescript client calling a node.js webserver talking to a MySQL DB. Had a successfully working call for names:
SELECT *
FROM stories JOIN users
ON stories.author_id=users.id
WHERE stories.story_id = 1;
If I need to encrypt the last names of authors at a field level in the DB, I could use AES_ENCRYPT and AES_DECRYPT. This changes the call to this. I used dlname here to distinguish the decrypted value from the lname field.
SELECT *, AES_DECRYPT(lname, UNHEX(SHA2('some phrase',512))) AS dlname
FROM stories JOIN users
ON stories.author_id=users.id
WHERE stories.story_id = 1;
This works in dev environment using phpMyAdmin, the dlname is fine on the return and shows as text. But when pushed to a prod env with MySQL and call it, the typescript (Angular 5 client) shows dlname as a blob [object Object]. I checked it with MySQL Workbench and it also shows it as a blob, which I can right-click on and then "Open value in viewer" and I see the last name just fine.
My question is, how do I write the typescript code to get the actual lname value out of the blob? The pertinent lines of code are...
this.http.get<StoryRes>(sRootURI + '/getStory/' + sDocID)
.subscribe(data => {
this.sAuthor = data[0].fname + ' ' + data[0].dlname;
In this case, the dlname shows in the console as [object Object].
I figured it out from some help found in BLOB data returned in MySQL using AES_DECRYPT with ORDER clause. Add a CAST(... AS CHAR) around your AES_DECRYPT function...
SELECT *, CAST(AES_DECRYPT(lname, UNHEX(SHA2('some phrase',512))) AS CHAR) as dlname...
On the client side, just make sure to call the new field name dlname instead of the actual name lname. Nothing additional is needed on the client side, will come through as text, as expected.

Removing URL encoding from Active Record query

I'm in over my head as always, but it's the only way I learn. Right now I am trying to query a column in a database for the current user and return the values. I'm using something like:
#tags = current_user.tags.select(:name).each { |p| p.name}
But it returns:
%5B%23%3CTag+name%3A+%22tag1%22%3E%2C+%23%3CTag+name%3A+%22tag2%22%3E%2C+%23%3CTag+name%3A+%22tag+test%22%3E%5D
From what I understand is that's Url Encoding. Is it possible to clean that up? I've tried using .delete or .gsub but I must be doing something wrong. Any insight? All my research on the site yields how to URL encode, but not URL decode.
For URI encoding/decoding you can take a look at rubyonrails.org:URI::Escape
For displaying HTML in Rails views check out the raw() method rubyonrails.org:ActionView::Helpers::OutputSafetyHelper