1

Background information

I'm working on my first node.js app using express. I have bug somewhere because I can't seem to match up the URI I'm specifying with the URI in my post method.

Code

router.post('/widget/:widgetnum/:rules', function(req, res, next) {

        var widgetnum = req.params.widgetnum;
        var rules = req.params.rules;
        if (debug) { winston.log('info', 'router.post) invoked with : ' + widgetnum)};
        if (debug) { winston.log('info', 'router.post) invoked with : ' + rules)};
        if (!valid_widget(widgetnum)) { 
                console.log('trigger 400');
                res.status(400).send("Invalid widgetnum");
        }
        if (rules==null)  {
                console.log('trigger 400');
                res.status(400).send("Invalid Time Conditions");
        }       
        res.send('hello');

});

And the address I'm testing with looks like this:

 http://myserver:3000/widget/123123/00:00:00_00:00:00_someothervalue

where I was hoping "123123" would be captured as the "widgetnum" and "00:00:00_00:00:00_someothervalue" as "rule"

I'm sure it's something very basic / simple that I'm missing. Thanks in advance for your time.

EDIT 1

Here's what I see on the commandline - proving that I am sending POST requests...

devserver:/var/www/widgettest# DEBUG=widgettest:* npm start

> widgettest@1.0.0 start /var/www/widgettest
> node ./bin/www

  widgettest:server Listening on port 3000 +0ms
POST /widget/11231231234/rules 404 713.167 ms - 1025
POST /widget/11231231234 404 75.216 ms - 1025
Happydevdays
  • 1,722
  • 2
  • 20
  • 48
  • 1
    it looks like you're making a GET request when your code defines a handler for a POST request. Checkout [postman](https://www.getpostman.com/) for a simple tool to test POST requests – mguida Oct 19 '16 at 18:38
  • I am using postman .. and I am POSTing for sure. I can see the requests on the cli and it shows POST. I will add this information to my question but out of curiousity, what made you think I'm doing a GET? – Happydevdays Oct 19 '16 at 18:42
  • 1
    I figured it out. I removed ''/widget/' from the path in the code and now it works – Happydevdays Oct 19 '16 at 18:48
  • Usually for a post request the data is in the request body, and a get request they will be query parameters. Take a look at the answers to http://stackoverflow.com/q/14551194/3711733 – mguida Oct 19 '16 at 18:48
  • @mguida hm. that's a good point about sending in the body. I will have to test that. – Happydevdays Oct 19 '16 at 19:41

0 Answers0