0

I use node express Router module, and it's route() method.

I need to accept an optional parameter, this way:

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

router.route('/verb/:optionalParameter').get(function(req, res, next) {
  // ...
}

How do I specify optionalParameter?
I did try:

router.route('/verb/:optionalParameter*?').get(function(req, res, next) {

and

curl -X GET -H "Accept: application/json" http://localhost:3000/verb/option1

works just fine, but

curl -X GET -H "Accept: application/json" http://localhost:3000/verb

spits a 404...

I'm sure I'm missing something obvious... :-( Any clue?

BrTkCa
  • 4,703
  • 3
  • 24
  • 45
MarcoS
  • 17,323
  • 24
  • 96
  • 174
  • Did you take a look to this? http://stackoverflow.com/questions/6784753/passing-route-control-with-optional-parameter-after-root-in-express – Mayas Sep 15 '15 at 14:33
  • Did you try to remove the asterics in the route url? Like so: `router.route('/verb/:optionalPatrameter?').get(function(req, res, next) {` instead of: `router.route('/verb/:optionalPatrameter*?').get(function(req, res, next) {` – Mayas Sep 15 '15 at 14:35
  • @Mayas: It's not about `express Router` module ... ;-( – MarcoS Sep 15 '15 at 14:36
  • Tried just now: same 404... :-( – MarcoS Sep 15 '15 at 14:37
  • You're right. Why don't you do this instead? `var express = require('express'); var router = express.Router(); var app = express() app.route('/verb/:optionalParameter?').get(function(req, res, next) { console.log('hello world') })` I just tried it and I get 'hello world' for /verb/1 and /verb/ as well. – Mayas Sep 15 '15 at 15:11

2 Answers2

1

Response it's 404 because don't exist route /verb, only /verb/:optionalParameter.

For works, it's need to create another route:

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

router.route('/verb/:optionalParameter').get(function(req, res, next) {
  // ...
}

// route localhost:3000/verb
router.route('/verb').get(function(req, res, next) {
  // ...
}

And try:

curl -X GET -H "Accept: application/json" http://localhost:3000/verb
BrTkCa
  • 4,703
  • 3
  • 24
  • 45
  • Well, not a real solution: in the first route it's a **mandatory** parameter... :-) I was asking for a way to write **one single** route... If it's not possible, I'll follow your advise... – MarcoS Sep 15 '15 at 14:39
  • I believe it's not possible, each route trigger one action. For another URL, require another route (get, put, post, delete). One URL passes a parameter and another does not. – BrTkCa Sep 15 '15 at 14:42
  • Ok. For go far away, consulting the documentation: http://expressjs.com/guide/routing.html – BrTkCa Sep 15 '15 at 14:50
0

Try this instead:

var express = require('express')
var router = express.Router()
var app = express()
app.route('/verb/:optionalParameter?').get(function(req, res, next) { console.log('hello world') })
Mayas
  • 1,434
  • 5
  • 16
  • 25
  • Can you summarize the differences between `express.Router().route()` and `express.route()` ? – MarcoS Sep 15 '15 at 15:15
  • http://expressjs.com/4x/api.html#app.route I guess the first is a deprecated way to do it. – Mayas Sep 15 '15 at 15:20