简体   繁体   English

index.js在在线服务器上不起作用

[英]index.js doesn't work on online server

I am trying to create a website. 我正在尝试创建一个网站。 The domain name should first go to index.js file. 域名应该首先进入index.js文件。 From there I load the main html file with app.get('/'). 从那里,我使用app.get('/')加载主要的html文件。 The file executes every function perfectly on localhost, but magically shows the content of the file in GoDaddy's hosting. 该文件可以在localhost上完美地执行所有功能,但可以在GoDaddy的托管中神奇地显示文件的内容。 Here's the code: 这是代码:

var express = require('express');
var mysql = require('mysql');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
app.use(express.static('public'));

var pool = mysql.createPool({
    // I specify the connection details here
});


pool.getConnection(function(error, tempCon) {
    if (!!error) {

        console.log("Error in the connection!");
    } else {
        console.log("Connected!");
    }
});


app.get('/', function(req, res){
    res.sendFile( __dirname + "/public/" + "website.html" );
});


var urlencodedParser = bodyParser.urlencoded({ extended: false })
app.post('/x', urlencodedParser, function(req, res){
    var query = "INSERT INTO Users (username, password, reason, phone, school, city, email) VALUES ('" + req.body.username + "', '" +req.body.password + "', '" + req.body.reason + "', '" + req.body.phone + "', '" + req.body.school + "', '" + req.body.city + "', '" + req.body.email +" '); ";
    pool.getConnection(function(error, tempCon) {
        if (!!error) {
            console.log("Error in the connection!");
        } else {

            tempCon.query(query);
            console.log(query);
        }
    });
});

what I am doing is the following 我在做什么是以下

var app = express();    
var bodyParser = require('body-parser');
var cons = require('consolidate');
var options = {maxAge : "1d"};

app.engine('html', cons.handlebars);

app.set('view engine', 'html');
app.set("views", __dirname + "/public");

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use("/js", express.static(__dirname + "/js", options));
app.use("/css", express.static(__dirname + "/css", options));
app.use("/images", express.static(__dirname + "/images", options));

app.get("/", function(req, res) {
    res.render("website", { });
});

Some of it may not be necessary for what you are doing, but you can remove what you don't need if you get it working. 对于您正在做的事情来说,其中某些可能不是必需的,但是如果您可以将其删除,则可以将其删除。

Also, the "/" is like a catch all pattern match, so a url like domain.com/data/x could be handled as following: 此外,“ /”就像是“捕获所有模式”匹配,因此可以按以下方式处理诸如domain.com/data/x之类的网址:

app.get("/:name/x", function(req, res) {
    res.render("x", {
            name : req.params.name
    });
});

app.get("/x", ... ) will match domain.com/x app.get(“ / x”,...)将匹配domain.com/x

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM