Selects with client-side nodejs+mysql

Asked

Viewed 59 times

-1

I’m new to nodejs, I’ve always used php with mysql and I’m migrating to Node but I have some problems, make the Node connect in the database works perfectly however I get no success when trying to run straight by Chrome returns the error Net.createConnection is not a Function, as I am not so aware I don’t know if I need a framework to manage the application so that the browser understands the select’s without error, to require them to work I am using Browserify my code js

 var $ = require('jquery');
 var mysql = require('mysql');

 var con = mysql.createConnection({
 host:'localhost',
 port:'0000',
 user:'root',
 password:'123456',
 database:'qualquer'
 });

con.connect(function(err) {
if (err) throw err;
 con.query("select * From qualquer.teste", function (err, result, 
fields) {
if (err) throw err;
console.log(result);
});
connection.end();
});""
  • But you’re trying to use one server plugin customer? It won’t work.

  • I am not, I want to use the serve so that the client can run when I run the application

  • Got it, you want to make session-based server equal to ASP and PHP, where the client only passes an HTTP request containing the session id. Have you met the Express

  • Yes in this parameter that I look for, hear about it I will take a better look at it recommend some site in particular ? I will be very grateful

  • The link I gave you is the official website in Portuguese. I believe this component helps you Express-Session

1 answer

1


There is a big difference between PHP and Node. PHP gives the illusion that you can run Mysql commands on the client side because PHP can have a mix of HTML, Mysql and PHP in the same file that seems to be served to the browser. But this is an illusion. In fact all PHP and Mysql code runs on the server and for the browser goes another version of this code, processed, without PHP or Mysql. That is, the browser does not (and should never) have access to Mysql directly.

What you need to do is create routes/urls where the Node can run code and send back a response. On these routes that are called when you navigate to a page on the server, or when you send an ajax request, you can pass data to the server to know what to do. In PHP this data is made available in $_GET or $_POST, on Node this works differently, but you can also access this type of data.

Having said that, it is worth mentioning that on Node it is a good idea to use a library to create the server, otherwise you will have to do a lot of things by hand that is complex. Test for example the express.js. library An example using express.js would look like this:

Example of requested url: http://localhost:3000/user/2?lang=en

Example code on Node:

app.get('/user/:id', function(req, res) {
  const userId = Number(req.params.id);
  con.query("SELECT username From users WHERE id=" + userId, function(err, result) {
    if (err) {
      console.log(err);
      res.send('Something went wrong...');
    }
    const lang = req.query.lang;

    if (lang === 'en') res.send('The user name is ' + result.name);
    else res.send('O nome do utilizador é ' + result.name);
  });
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.