index.js不适用于在线服务器

时间:2018-04-07 00:18:44

标签: javascript node.js web

我正在尝试创建一个网站。域名首先应该转到index.js文件。从那里我用app.get(' /')加载主html文件。该文件在localhost上完美地执行每个函数,但神奇地在GoDaddy的托管中显示文件的内容。这是代码:

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);
        }
    });
});

1 个答案:

答案 0 :(得分:0)

我正在做的是以下

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", { });
});

对于你正在做的事情,有些可能没有必要,但你可以删除你不需要的东西。

此外,“/”类似于捕获所有模式匹配,因此像domain.com/data/x这样的网址可以按如下方式处理:

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

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