我的NodeJs App出现在终端中但没有通过url连接到数据库的任何原因是什么?

问题描述

我目前正在尝试使我的子域网站在线。该站点在本地以及数据库上都可以运行,但是在联机状态下,该站点可以正常运行但未连接到数据库。我正在使用Namecheap提供的共享托管,并在cPanel中使用NodeJS App设置。我可以通过终端而不是URL连接到终端和数据库。我不确定这是代码,端口问题还是其他问题。我也没有收到错误。该站点根本无法连接到数据库

我是后端节点和Express的新手,所以大部分代码都是基于我使用Express文档所遵循的教程。在本地,注册表单会将用户添加数据库中并返回首页,尽管速度很慢。在服务器上,它似乎需要一个页面,但是从不向数据库添加用户。下面是我的脚本:

var express = require('express');
    var app = express();
    var path = require('path');
    const hostname = '127.0.0.1';
    const port = 3000;
    
    app.listen(port,hostname,() => {
        console.log(`Server running at http://${hostname}:${port}/`);
    });
    
    app.use(express.static(path.join(__dirname + "/public")));
    
    app.get('/index.html',function (req,res,next) {
        res.sendFile(__dirname + '/index.html');
        console.log("got index page");
    });
    
    app.use(express.urlencoded({
        extended: true
    }))
    
    app.get('/signup.html',next) {
        console.log("got signup page");
    });
    
    var MysqL = require('MysqL');
    var con = MysqL.createConnection({
        host: 'localhost',user: '******',password: '******',database: '*******'
    });
    
    con.connect(function (err) {
        if (err) throw err;
        console.log("Connected to db");
    });
    
    
        app.post('self',(request,response) => {
            const username = request.body.username
            const password = request.body.password
            const email = request.body.email
            const gender = request.body.gender
    console.log(email);
    console.log(username);
    console.log(password);
      
            var sql = "INSERT INTO `accounts` (username,password,email,gender) VALUES ('" + username + "','" + password + "','" + email +"','"+gender+"')";
            
        con.query(sql,function (err,result) {
            if (err) throw err;
            console.log("1 record inserted");
          
        });
         return request.redirect("/https://matcher.withaliquid.com/");
         });
    
    function getHomePage() {
        app.use(express.static(__dirname + '/'));
        app.get('/',next) {
        res.sendFile(__dirname + '/index.html');
    });
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)