Mornor
Mornor

Reputation: 3783

Express middleware implies a request timeout

I have a middleware in my Express/Node.js app to protect a specific route.

The thing is, when I use this middleware, I get a Request timeout if the credentals check is wrong

Here is the code I currently use :

var express = require('express');
var bodyParser = require('body-parser');

// Set Header because cross-domain call
app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://docker.local");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");
  next(); 
});  

function checkAuth(req, res, next){
  if(// Credentials right){
       next(); 
  } else {
       // If code enter in that case, it will generate a Timeout
       // even with next(); ...
  }
}

// Use checkAuth to protect route
app.get('/server', checkAuth, getMessages);
app.get('/', function(req, res){
  res.status(200).send('Slack Server is listening for outgoing webhooks ...');
});

Question is: why do I get this timeout?

EDIT

If I make the GET request using a browser, then I have the request timeout. However, if I trigger the GET request in another way, then I get no timeout and the code works. Is it possible that the GET /favicon somehow mess this stuff up?

Upvotes: 0

Views: 215

Answers (1)

Ben Fortune
Ben Fortune

Reputation: 32127

app.use requires you to call next to continue to the next middleware or request in the stack.

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://docker.local");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Authorization, Content-Type, Accept");
  next();
});

Reference

Upvotes: 5

Related Questions