节点。js访问WampServer MySQL数据库

时间:2021-11-06 18:11:17

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. ;)

我是node的新手。js和MySQL,当我试着同时学习这两个的时候……我们就说我需要帮助。,)

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

我想使用node-mysql模块通过node.js动态编辑数据库。所有的基本代码都已就绪。

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?

问题是,我正在监听端口8080,而localhost当然是端口80。我应该听80端口吗?如果是的话,我该怎么做而又不会弄得一团糟呢?如何访问我用PHPmyAdmin创建的数据库?

1 个解决方案

#1


4  

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

WAMP提供了很多东西,包括MySQL和一个预先配置了phpMyAdmin的apache web服务器。

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

默认情况下,Apache web服务器监听端口80,MySQL服务器监听端口3306。随着WAMP的运行,这些端口将被占用。只要在端口8080上没有其他进程监听,您的节点进程就能够创建一个监听端口8080的服务器。默认情况下,这是可以的,您将能够通过http://localhost:8080访问节点http服务器。

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.

在端口3306上建立了与MySQL数据库的连接。您只需要像通常通过phpMyAdmin那样设置数据库。默认情况下,这将位于端口80上的apache服务器上运行的http://localhost/phpMyAdmin。

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

澄清一下,你的术语有点混乱。主机名中的Localhost。这是你想要与之交谈的机器的位置。端口号是完全独立的,“localhost当然是端口80”没有任何意义。您可以为localhost指定任何有效的端口号。如前所述,监听端口8080意味着可以通过http://localhost:8080访问节点服务器

#1


4  

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

WAMP提供了很多东西,包括MySQL和一个预先配置了phpMyAdmin的apache web服务器。

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

默认情况下,Apache web服务器监听端口80,MySQL服务器监听端口3306。随着WAMP的运行,这些端口将被占用。只要在端口8080上没有其他进程监听,您的节点进程就能够创建一个监听端口8080的服务器。默认情况下,这是可以的,您将能够通过http://localhost:8080访问节点http服务器。

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.

在端口3306上建立了与MySQL数据库的连接。您只需要像通常通过phpMyAdmin那样设置数据库。默认情况下,这将位于端口80上的apache服务器上运行的http://localhost/phpMyAdmin。

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

澄清一下,你的术语有点混乱。主机名中的Localhost。这是你想要与之交谈的机器的位置。端口号是完全独立的,“localhost当然是端口80”没有任何意义。您可以为localhost指定任何有效的端口号。如前所述,监听端口8080意味着可以通过http://localhost:8080访问节点服务器