节点js服务器"/>
如何在cpanel中设置连接mysql db的节点js服务器
我是一个新手,试图在cpanel中启动一个仅连接到mysql数据库的节点js服务器,但是当我包含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');
回答如下:您已经创建了服务器,并且正在根据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');
更多推荐
如何在cpanel中设置连接mysql db的节点js服务器
发布评论