Skip to content

v7 middleware functions no longer have server context (undocumented breaking change) #1639

@davidmarkclements

Description

@davidmarkclements
  • Used appropriate template for the issue type
  • Searched both open and closed issues for duplicates of this issue
  • Title adequately and concisely reflects the feature or the bug

Bug Report

Restify Version

v7.1.0
v7.0.0

Node.js Version

v8.11.1

Expected behaviour

If the breaking change is not deliberate:

The this value of a middleware function should be the server instance

If the breaking change is deliberate:

The changelog and migration guide (http://restify.com/docs/6to7/) should document the change

Actual behaviour

The this value is undefined (or global in non-strict context)

Repro case

'use strict'

const restify = require('restify')
const server = restify.createServer()

server.use(function (req, res, next) {
  console.assert(this === server, 'function context is server instance')
  next()
})

server.get('/', function (req, res, next) {
  res.send('hello')
  next()
})

server.listen(3000)

Cause

handler isn't called with server instance in the call function in chain.js (Chain would have to be instantiated with a server instance passed as an option)
https://github.com/restify/node-restify/blob/master/lib/chain.js#L160
https://github.com/restify/node-restify/blob/master/lib/chain.js#L164

Or else self should be bound to handler in the use method:
https://github.com/restify/node-restify/blob/master/lib/server.js#L506

Are you willing and able to fix this?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions