无法通过Node.js

时间:2017-11-02 21:07:06

标签: mysql node.js express xampp ip

我在运行OS X(标准安装)的计算机上安装了XAMPP。我启动了XAMPP并点击了"开始"常规选项卡中的按钮。状态指示灯变为绿色。

我尝试连接到这样的服务:

const express = require('express')
const mysql = require('mysql')

// Create Connection
const db = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '1234',
    //database: 'node-mysql'
}) 

// Connect
db.connect((err) => {
    if(err){
        throw err
    }
    console.log('MySql Connected...')
})

const app = express()

// Create DB
app.get('/creatdb', (req, res) => {
    let sql = 'CREATE DATABASE node-mysql'
    db.query(sql, (err, result) => {
        if (err) throw err
        console.log(result)
        res.send('Database created')
    })
})

app.listen(3000, () => console.log('running on 3000'))

我看到了这个错误:

<path>/app.js:15
        throw err
        ^

Error: connect ECONNREFUSED 127.0.0.1:3306
    at Object._errnoException (util.js:1024:11)
    at _exceptionWithHostPort (util.js:1046:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1182:14)
    --------------------
    at Protocol._enqueue (/Users/casey/Dev/mysql/node_modules/mysql/lib/protocol/Protocol.js:145:48)
    at Protocol.handshake (/Users/casey/Dev/mysql/node_modules/mysql/lib/protocol/Protocol.js:52:23)
    at Connection.connect (/Users/casey/Dev/mysql/node_modules/mysql/lib/Connection.js:130:18)
    at Object.<anonymous> (/Users/casey/Dev/mysql/app.js:13:4)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)

在XAMPP的“常规”选项卡中,IP地址显示为192.168.64.2。也许它需要是localhost(127.0.0.1)。我怎么设置这个?当我在浏览器中转到192.168.64.2时,我被重定向到http://192.168.64.2/dashboard/

的XAMPP仪表板

XAMPP是否运行在错误的IP地址,如果是,我该如何更改?如果问题不是IP,那么我是否知道如何追踪问题呢?感谢。

0 个答案:

没有答案