我在应用程序上工作,我必须将我的应用程序与某些服务器上的数据库连接。我的应用程序连接到服务器上的数据库(cPanel)时遇到问题。在我的文件server.js中我有这段代码
var express = require('express');
var cors = require('cors');
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql'); //npm install mysql
var rp = require('request-promise'); // npm install --save request-promise
var request = require('request'); //npm install --save request
let router = require("express").Router();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
var port = process.env.PORT || 8080;
var apiRoutes = express.Router();
apiRoutes.get('/', function (req, res) {
res.json({ message: 'Node works' });
})
var pool = mysql.createPool({
host: "1.222.333.444",
user: "dbuser",
password: "qwertz",
port: 3306,
database: "dbtest"
});
pool.getConnection(function (err, connection) {
if (err) {
console.error("Error: " + err);
}
else {
console.log("Connected to database!");
}
});
app.use('/api', apiRoutes);
app.listen(port);
console.log('Api works');
我使用nodemon启动应用程序并收到此错误