KabG
KabG

Reputation: 25

Simple configurable Express middleware not executing

I'm trying to build a configurable middleware (a custom middleware function that takes custom parameters, and returns a (req,res,next) function that uses these parameters) in Express. However, I can't get this middleware to execute on a router.get() call.

The expected behaviour in the code below is that, when trying to GET the home page (/), the console logs 'success!' then I get a 404 (as the next() in the middleware will pass me on to the 404 function).

However, nothing happens and the request times out. The function is being detected, as when I put the console.log() in the middleware but just before the return function(req,res,next){ line, I am seeing the output on the console. I only get this issue when the configurable middleware is returning a function.

./app.js (no changes to the template that Express builds):

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

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

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
  // set locals, only providing error in development
  res.locals.message = err.message;
  res.locals.error = req.app.get('env') === 'development' ? err : {};

  // render the error page
  res.status(err.status || 500);
  res.render('error');
});

module.exports = app;

./routes/index.js:

var express = require('express');
var router = express.Router();

var simplemiddleware = require('../middlewares/simplemiddleware');

/* GET home page. */
router.get('/', function(req, res, next) {
  simplemiddleware.simpleReturn('success!');
});

module.exports = router;

./middlewares/simplemiddleware.js:

exports.simpleReturn = function (outputString){
  return function (req,res,next) {
    console.log(outputString);
    next();
  }  
}

Upvotes: 1

Views: 453

Answers (1)

Rick Su
Rick Su

Reputation: 16440

Please note that the

var simplemiddleware = require('../middlewares/simplemiddleware');

actually is a function, to fix the problem, please try the following.

./routes/index.js:

var simplemiddleware = require('../middlewares/simplemiddleware');

/* GET home page. */
router.get('/', function(req, res, next) {

  // note `simplemiddleware.simpleReturn(str)` returns a function
  let func = simplemiddleware.simpleReturn('success!');

  // run the middleware
  func(req, res, next);
});

Upvotes: 2

Related Questions