Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

javascript - How do I pass a variable from client side NodeJS to server side?

I have constructed a variable containing the SQL statement in my client-side

var sql = "SELECT * FROM restaurant WHERE (areaCategory" + " " + area + ")" + "AND (cuisineCategory" + " " + cuisine + ") AND (priceCategory" + " " + price +")"

How can I export this SQL statement from client-side to server-side in order to do send this statement? The SQL statement varies depending on situations, hence I have to create a variable.

question from:https://stackoverflow.com/questions/65914330/how-do-i-pass-a-variable-from-client-side-nodejs-to-server-side

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, as mentioned by @Aley, You really don't want a client to have full access to your database!

Instead I would send the params to the server using an AJAX call or a form, then use prepared statements on server side

AJAX

You might want to use a library like axios and make a Ajax call with post method:

//client side
axios.post('/restaurant', {
    area: areaCategory,
    cuisine: cuisineCategory
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });

Forms

Forms should be self-explanatory

<!--client side-->
<form method="post" action=/restaurant">

<input type="text" name="area" placeholde="Area…">
<input type="text" name="cuisine" placeholde="Cuisine……">
<input type="submit">
</form>

Prepared statements

As there are many different databases with different interfaces, here are some links:

Does SQLite3 have prepared statements in Node.js?

Preventing SQL injection in Node.js

How do I create a prepared statement in Node.JS for MSSQL?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...