-1

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?

6
  • 4
    You need to learn the difference between server-side code and client-side code. Commented May 10, 2013 at 16:43
  • 13
    yeah, go ahead and stick some javascript in your html document that exposes the host, user, password, and db name of a db on your server. what could go wrong? Commented May 10, 2013 at 16:44
  • Possibly stackoverflow.com/questions/9901082/… Commented May 10, 2013 at 16:44
  • 8
    Do not store passwords in plain text Commented May 10, 2013 at 16:45
  • 2
    You probably don't want client browser to have full access to your database. Commented May 10, 2013 at 16:45

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

thank, so how can I run my code, I mean how to send some arguments to server.js and back ?
@user1726810: You need to use AJAX or WebSockets.
@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 "Ignore everyone suggesting browserify" ? Nobody here ever suggested using browserify. If you targeted my answer, I'd suggest you reread it.
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.

Comments

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.

Comments

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>

3 Comments

The require() is the least of his problems here.
@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.
@Juhana I'm not sure if those exist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.