0

Is it possible to run this JavaScript code in .html document:

<script>
    function DBConnect() {
        var mysql = require('mysql');
        var mydb = mysql.createConnection({
            host: 'localhost',
            user: 'root',
            password: 'admin123',
            database: 'users'
        });

        var username = user_name.value;
        mydb.connect();

        var query = ('select passwd from peer where username=' + username);

        console.log(query);

        connection.end(function(err) {
            // The connection is terminated now
        });
    }
</script>

Because when I'm trying always got an error: Undefined "require", or how can I call this function in other f.e.: db.js? I have already script server.js, that is running from Node.js, do you think the code above should be running here?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Patrik18
  • 329
  • 5
  • 9
  • 18

4 Answers4

4

Ignore everyone suggesting browserify. That would make sense if you had a basic understanding of the client-server architecture, and hence, had an intuition for the limitations of browserify. The answer to your question is

No.

Node.js code runs on the server. In your code you are doing things that cannot be done on the client. You should probably understand why before attempting to write code that handles sensitive information.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • thank, so how can I run my code, I mean how to send some arguments to server.js and back ? – Patrik18 May 10 '13 at 17:03
  • @user1726810: You need to use AJAX or WebSockets. – SLaks May 10 '13 at 17:04
  • 2
    @user1726810 I personally recommend going to college, but many developers have successfully learned without schooling in the field. I can't responsibly give you any more specific help when you don't understand why you shouldn't be putting database admin credentials in javascript. (Hint: view page source...) – djechlin May 10 '13 at 17:07
  • @djechlin "Ignore everyone suggesting browserify" ? Nobody here ever suggested using browserify. If you targeted my answer, I'd suggest you reread it. – Denys Séguret May 13 '13 at 16:21
2

Using browserify, you can run some Node.js modules on the browser, but one doing such heavy I/O can't run in your browser.

So no, you can't simply take this Node.js code and try to run it in the browser. You'll have to design a proper client-server application, with the server doing all accesses to the database.

A solution for which you'd get many tutorials could for example be

  • To let your browser JavaScript code issue some Ajax requests
  • To let your server answer those requests by querying the database and send some data in JSON in the response.
  • To make the browser change the HTML using this JSON.

You could start by googling "json node.js ajax mysql", but you have a lot of study facing you.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
2

Even if it was possible to connect to the MySQL database directly from the HTML, it is a bad idea. If you include the database credentials in the HTML, anyone who has access to that webpage can access your database and do whatever they want with it. This is a bad idea.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Davies
  • 41
  • 1
  • 6
0

You are probably trying to execute Node.js JavaScript code in a browser and the require keyword isn't recognized.

To import a JavaScript file in a browser, you should use something like this:

<script src="path/to/your/JavaScript/file"></script>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
MastErAldo
  • 634
  • 3
  • 12
  • 29
  • 5
    The `require()` is the least of his problems here. – JJJ May 10 '13 at 16:47
  • 1
    @user1726810 There is no need for it. The file has access to all global variables as any other script. As others have suggested, you need to "un-learn" node before you can start with web scripting--I suggest you grab a good web-dev centric JavaScript book. – JJJ May 10 '13 at 17:02
  • @Juhana I'm not sure if those exist. – djechlin May 10 '13 at 17:23