Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

res.send and res.json not available when using router without express? #39

Closed
sjanuary opened this issue Mar 7, 2016 · 2 comments
Closed
Assignees
Labels

Comments

@sjanuary
Copy link

sjanuary commented Mar 7, 2016

I've been working on an example for the docs for #36 but the following didn't work for me:

var finalhandler = require('finalhandler')
var http         = require('http')
var Router       = require('router')

var router = Router()
router.get('/', function (req, res) {
  res.setHeader('Content-Type', 'text/plain; charset=utf-8')
  res.json({link: '/abc'})
  res.end()
})

var server = http.createServer(function(req, res) {
  router(req, res, finalhandler(req, res))
})

server.listen(3000)

Instead I got TypeError: undefined is not a function

It seems like res.json and res.send aren't available if a Router is used standalone without Express. Is this by design? I couldn't find anything about it in the docs or in past issues.

@dougwilson
Copy link
Contributor

This is by design, as those are not part of routing a request. The router just routes requests to handles. Anything the router provides should be in the README in the repository (and it doesn't call out to everything not included, as that would be a pretty much endless list--if it's not in the docs, it can be assumed to not exist).

With the router, your req and res will simply look like however they looked when you passed them into the main router function. In your example, you are passing what comes from http.createServer, so the methods on those objects are documented in node.js:

https://nodejs.org/api/http.html

@dougwilson dougwilson self-assigned this Mar 7, 2016
@sjanuary
Copy link
Author

sjanuary commented Mar 8, 2016

Thanks @dougwilson, that makes sense.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants