如何在cpanel中设置连接mysql db的节点js服务器

编程入门 行业动态 更新时间:2024-10-07 05:26:28

如何在cpanel中设置连接mysql db的<a href=https://www.elefans.com/category/jswz/34/1771452.html style=节点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服务器

本文发布于:2024-05-07 09:15:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1755558.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:节点   服务器   如何在   cpanel   db

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!