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

camp.route is not documented #50

Open
techtonik opened this issue Oct 19, 2016 · 3 comments
Open

camp.route is not documented #50

techtonik opened this issue Oct 19, 2016 · 3 comments

Comments

@techtonik
Copy link

techtonik commented Oct 19, 2016

I fail to find documentation for camp.route params needed to understand shields.io code (badges/shields#811). There is a reference to some "Diving In" section at https://github.com/espadrine/sc/blob/master/doc/Readme.md#ask-and-augmented-request but search doesn't show anything,

@jankeromnes
Copy link
Collaborator

jankeromnes commented Oct 19, 2016

Hi @techtonik, I'm not sure either what this "Diving in" reference is for (reminds of the unrelated Walkthrough), but for what it's worth I know and use camp.route.

Note: It's progressively being deprecated in favour of camp.path (and Ask is also slowly being deprecated), which might be why the documentation is gone.

As mentioned in the docs, the parameters are camp.route(pattern, function (query, path, end, ask) {}) (I prefer to call them app.route(regex, (data, match, end, query), which might or might not be more intuitive to you):

  • pattern: a regex like /^\/about\/?$/
  • query: data extracted from the incoming request (URL query parameters like ?page=4, urlencoded POST parameters, or multipart data from <form> submits which can include uploaded files)
  • path: actually the matched results from the regex, e.g. [ '/about' ], or [ '/ABC', 'B', 'C' ] if your regex is /^\/A(\w)(\w)$/
  • end: the callback you can use to serve a response. Its parameters are end(templateData, { template: '/path/to/template-file.html' })
  • ask: actually just a container for req and res. In some cases you might prefer to just call ask.res.statusCode = 404; ask.res.end('not found') instead of using the end() callback

@techtonik
Copy link
Author

In shields code it seems that query comes decoded - instead of ?page=4 it can be accessed as query.page? Is that right? In that case it will be better to rename it to params meaning "URL params extracted from query", because data is too generic to have any context meaning.

@jankeromnes
Copy link
Collaborator

jankeromnes commented Oct 19, 2016

That's true, everything in query is decoded (URL query parameters are parsed, POST body parameters are urldecoded, and multipart form data from <form> submits are re-assembled).

Edit: For what it's worth, I also like the name params for this argument, and it's more precise than the data alias I use.

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

No branches or pull requests

2 participants