Как настроить сервер node js, который подключает mysql db в cpanel - PullRequest
0 голосов
/ 09 мая 2020

Я совершенно новичок ie пытаюсь запустить сервер node js в cpanel, который просто подключается к mysql db, тем не менее, когда я включаю биты mysql, они полностью игнорируются, никаких ошибок или ссылок на mysql вообще. Есть идеи?

const http = require('http');
var mysql = require('mysql');

var con = mysql.createConnection({
   host     : 'localhost',
   user     : 'admin',
   password : 'password',
   database : 'members',
   port:3306
});
// Create an instance of the http server to handle HTTP requests
let app = http.createServer((req, res) => {
    // Set a response type of plain text for the response
    res.writeHead(200, {'Content-Type': 'text/plain'});

    // Send back a response and end the connection
    res.end('Hello World!\n');   
    con.connect(function(err) {
    if (err) throw err;
    res.end('Connected!');
    }); 

});

// Start the server on port 3000
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');

1 Ответ

0 голосов
/ 09 мая 2020

Вы создали сервер и устанавливаете соединение по запросу HTTP API, и по этой причине вы ничего не видите в командной строке. Попробуйте ввести localhost: 3000 на почтальоне, и вы увидите соединение. Есть еще одно исправление, которое следует избегать отправки ответа перед подключением к базе данных.

const http = require('http');
var mysql = require('mysql');

var con = mysql.createConnection({
host     : 'localhost',
user     : 'admin',
password : 'password',
database : 'members',
port:3306
});
// Create an instance of the http server to handle HTTP requests
let app = http.createServer((req, res) => {
// Set a response type of plain text for the response
res.writeHead(200, {'Content-Type': 'text/plain'});

// Send back a response and end the connection
//res.end('Hello World!\n'); // comment this line   
con.connect(function(err) {
if (err) throw err;
res.end('Connected!');
}); 

});

// Start the server on port 3000
app.listen(3000, '127.0.0.1');
console.log('Node server running on port 3000');

Надеюсь, это поможет! Пожалуйста, дайте мне знать!

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...