admin管理员组文章数量:814879
如何在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服务器
版权声明:本文标题:如何在cpanel中设置连接mysql db的节点js服务器 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1715136131a821439.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论