简体   繁体   中英

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. From there I load the main html file with app.get('/'). The file executes every function perfectly on localhost, but magically shows the content of the file in GoDaddy's hosting. 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:

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

app.get("/x", ... ) will match domain.com/x

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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