paulc1111
paulc1111

Reputation: 651

Trying to get query in app.get function using express in node.js

I'm trying to get query after 'status.html' such as localhost:3000/status.html?channel="string that I want to get".

So I wrote the code like below.

app.get('/status.html', isLoggedIn, function(res, req){
  if (req.query.channel){
    if (req.isAuthenticated()){
      res.redirect('/status');

    }else{
      console.log("Please Log in to access to this webpage");
      res.redirect('/login');

    }
  }
});

However, it doesn't even go through app.get '/status.html'...

What is my problem here...? Can anyone help me out here..?

Thank you

Just in case, my directory is set as below

node_modules
public
  images
  javascripts
  js
  stylesheets
  status.html
routes
views
  login
app.js
package.json

Upvotes: 0

Views: 151

Answers (2)

parwatcodes
parwatcodes

Reputation: 6798

app.get(path, callback) is a route

a path can be a string a a regex pattern

// if you use `/status.html` then html `href` should as be same path
// `/status.html`
// app.get('/status.html', isLoggedIn, function(res, req){

app.get('/status', isLoggedIn, function(res, req){
      if (req.query.channel){
        if (req.isAuthenticated()){
          res.redirect('/status');

        }else{
          console.log("Please Log in to access to this webpage");
          res.redirect('/login');
        }
      }
    });

in your html file you have to create an anchor tag with href

html file

// <a href ="/status.html" class="btn btn-default btn-sm">Status</a>
<a href ="/status" class="btn btn-default btn-sm">Status</a>

when you click the status button it will call the /status route app.get()

// `/status.html` is just a route path it has nothing to do with html files

Upvotes: 1

Muhammad Ali
Muhammad Ali

Reputation: 2014

Please remove the file status.html from your public directory and create route , basically it is requesting to your public directory, If you want to use status.html in public directory, you can implement ajax work there.

Upvotes: 0

Related Questions