ExoSkeleton321 ExoSkeleton321 - 3 months ago 45
Jade Question

Check for a GET variable in URL

so I'm making a simple log in form in my site using Jade and ExpressJS. It's all working, the only problem I'm having is when the user enters incorrect log in details. When they enter wrong details I redirect them to the log in page with a parameter in the URL (

). My question is: how can I check if this GET variable exists in my jade view?

This is my code:


form(action="/log-in" method="post")
label Username
input(type="text" class="form-control" name="username" placeholder="Username")
label Password
input(type="text" class="form-control" name="password" placeholder="Password")
input(type="submit" class="btn btn-info pull-right" style="margin-top: 15px;" value="Log In")


app.get("/admin",function(req, res){

res.render("admin", {session_name: req.session.userName});


app.post("/log-in", function(req, res){

var admin = Admin.findOne({"username": req.body.username, "password": req.body.password});
admin.count(function(error, count){
if(count > 0){
req.session.userName = req.body.username;

I tried the following inside my jade view but get an error
Cannot read property 'location' of undefined

- var page = window.location.pathname

if page === "/admin?falseLogIn"
.alert.alert-danger Error! Wrong details, please try again!

I come from a PHP background, so in PHP i would be able to check directly in the html using:

<?php if(isset($_GET['falseLogIn'])){// display error to user } ?>

Any help is greatly appreciated. Thanks!

Dan Dan

First, I would recommend using full key-values for your query string, /admin?login=false

Then you use req.query to get the specific variable and pass into your jade view or using res.locals to make the variable accessible in your views.


app.get("/admin",function(req, res){

  res.locals.login = req.query.login; // this would be accessible in your view as 'login'

  if (req.session.userName) {
    res.render("admin", {session_name: req.session.userName});
  } else {
    res.render("log-in", {
      login: req.query.login, // or pass variable to your view directly, also accessible in your view as 'login'

Reasoning. I don't think it's good practice to parse your url query directly in your view. While that is fine for raw php like your example using $_GET, if you use a MVC framework, like Laravel for example, you quickly move away from that. Let the controller do its job of handling logic based on queries and handing off the relevant information to views. Then in your view, its should be simple presentation logic.