Friday 9 June 2017

res.redirect, res.sendFile not working

The res.redirect to 'http://localhost:3000/hello.html' is not working. I have also tried 'hello.html' instead of 'http://localhost:3000/hello.html'. The hello.html and index2.html files are in the Public folder. I even get the 'in channel1' log on the console. Any help is appreciated. Thank you.

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>



via ARPIT PRASHANT BAHETY

No comments:

Post a Comment