res.redirect,res.sendFile无效

时间:2017-06-10 04:34:49

标签: javascript node.js express redirect

res.redirect转向' http://localhost:3000/hello.html'不管用。 我也试过' hello.html'而不是' http://localhost:3000/hello.html'。 hello.html和index2.html文件位于Public文件夹中。我甚至得到了频道1'登录控制台。任何帮助表示赞赏。谢谢。

index.js:

var express = require("express");
var app = express();
var mysql = require("mysql");
var bodyParser = require('body-parser');
var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "admin123",
  database: "Db"
});
app.use(express.static('./Public'));
app.get('/', function(req, res) {
res.redirect('index2.html');
});

con.connect(function(err){
  if(err){
    console.log('Error connecting to Db');
    return;
  }
  console.log('Connection established');
});

app.get('/channel1', function(req, res) {
    console.log("in channel1");
    res.redirect('http://localhost:3000/hello.html');
});

con.end(function(err) {
  // The connection is terminated gracefully
  // Ensures all previously enqueued queries are still
  // before sending a COM_QUIT packet to the MySQL server.
});

var server = app.listen(3000, function() {
    console.log("Server is running");
});

index2.html:

<html>
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <link rel="stylesheet" href="index.css">
        <script src="jquery-3.1.1.min.js"></script> 
    </head>
    <body>
        <div class="container-fluid">
            <div class="row">
                <h1 align="center">First Page</h1>
            </div>
            <div class="row">
                <div class="col-md-4">
                    <button class ="btn btn-primary btn-lg btn-block c1" onclick="fun1(1)">1</button>
                </div>
            </div>
</body>
    <script>
        function fun1(num) {
            $.get('/channel1', function() {
                console.log("in channel1");
            });
        }
    </script>
</html>

0 个答案:

没有答案