3

I'm really new to node.js and MySQL, and when I try to learn both at once... let's just say I need some help. ;)

I want to use the node-mysql module to dynamically edit a database via node.js. All the basic code is in place.

var http = require('http'),
   mysql = require("mysql");

var connection = mysql.createConnection({
   user: "root",
   password: "",
  database: "ballot"
});

http.createServer(function (request, response) {

  request.on('end', function () {

      connection.query('SELECT * FROM data;', function (error, rows, fields) {

        response.writeHead(200, {
            "Content-Type": "text/plain",
            'Access-Control-Allow-Origin' : '*'
        });
        response.write(JSON.stringify(rows));
        response.end();

      });
   });

}).listen(8080);

The problem is, I'm listening on port 8080, and localhost is of course port 80. Should I listen on port 80? If so, how do I do so without messing with Wamp? And how can I access the databases I create with PHPmyAdmin?

Jack Guy
  • 8,346
  • 8
  • 55
  • 86

1 Answers1

4

WAMP gives you a number of things, including MySQL and an apache web server with phpMyAdmin pre-configured.

By default the Apache web server listens on port 80 and the MySQL server listens on port 3306. With WAMP running, these ports will be taken. Your node process will be able to create a server listening on port 8080 as long as you have no other processes listening on port 8080. By default this should be fine and you will be able to access the node http server via http://localhost:8080

A connection with the MySQL database is established on port 3306. You just need to setup your database as you normally would through phpMyAdmin. By default this will be at http://localhost/phpMyAdmin which is running on the apache server on port 80.

Just to clarify, as your terminology seems slightly confused. Localhost in a host name. It's the location of the machine that you wish to talk with. The port number is completely separate and "localhost is of course port 80" doesn't make any sense. You can specify any valid port number for localhost. As I already mentioned, listening on port 8080 means you can access the node server through http://localhost:8080

Matt Esch
  • 22,661
  • 8
  • 53
  • 51
  • Sorry, I meant that WAMP operates on port 80. So are you saying that I can just specify "host: 'http://localhost/phpMyAdmin'" as a parameter when I create the MySQL connection? – Jack Guy Oct 13 '12 at 13:49
  • No I'm saying that you have nothing to worry about because mysql operates on port 3306 by default, not 80 or 8080. WAMP operates on many ports, it's a collection of programs. The web-portion of WAMP (apache) operates on port 80. The MySQL database operates on port 3306. phpMyAdmin runs on port 80 as a web application, the php application itself creates a connection to localhost:3306. Your node program will also connect to MySQL at localhost on port 3306 by default. – Matt Esch Oct 13 '12 at 13:57