Express middleware not firing when using postman but works when connecting with browser

I have a simple express application boiler plated with Express Generator.

I am trying to run middleware to verify a JWT but none of my middleware seems to get run when testing with postman.

If I make a request from my browser, I can see the logs in the middleware run fine.

My app.js has a simple logger function which should run on all requests.

import token_verification from './middleware/token_verification';
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var index = require('./routes/index');
var api = require('./routes/api');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, '../views'));
app.set('view engine', 'jade');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', index);
app.use('/api', api);

var myLogger = function (req, res, next) {


const LOCALHOST = process.env.PORT || 3037;
app.listen(LOCALHOST, () => {
  console.log(`Listening on ${LOCALHOST}`);

module.exports = app;

I have my routes/index.js with a simple GET route

import express from 'express';
const router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });

module.exports = router;

As I said, when I got to http://localhost:3037/ in my browser, I can see my terminal logs "LOGGED" but when I make a GET request via post to the same URI, nothing is logged and appears no middleware runs.


Postman screenshot


1 answer

  • answered 2018-01-13 18:55 Paul

    I don't know why it's working from the browser but not from Postman (there's likely some headers that are being set by the browser and not by postman, but I don't see why that would matter).

    What does matter, though, is that you're defining your logger after your routes, and since your GET route ends the response with res.render the middleware will not fire.

    Remember, in the latest versions of Express, middleware fires in the order they're mounted on the app.

    If you want the logger to always fire, reorder things:

    var myLogger = function (req, res, next) {
    app.use('/', index);
    app.use('/api', api);