Seven
Seven

Reputation: 391

Node Express - empty query after form submit

I would like to pars url after form submit. I have simple form:

form(method='post', action='/recipe/create')
  hr
    div
      div.input.text
        label(for='recipeTitle') Tytuł przepisu:
        input(type='text', name='recipeTitle', id='recipeTitle')
      div.input.text
        label(for='photoFileName') Nazwa zdjęcia:
        input(type='text', name='photoFileName', id='photoFileName')

After submit this code is executed.

exports.create = function(req, res){
   var url = require('url'); 
   var url_parts = url.parse(req.url, true);   
   console.log(url_parts);

My question is why console shows empty query

{ protocol: null,
  slashes: null,
  auth: null,
  host: null,
  port: null,
  hostname: null,
  hash: null,
  search: '',
  query: {},
  pathname: '/recipe/create',
  path: '/recipe/create',
  href: '/recipe/create' }

Upvotes: 0

Views: 1435

Answers (1)

gustavohenke
gustavohenke

Reputation: 41440

This happens because you're posting to an URL which doesn't have a query string - /recipe/create.

Also, you seem to be using Express, which will give you the current query string already parsed:

// GET /search?q=tobi+ferret
req.query.q
// => "tobi ferret"

// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
req.query.order
// => "desc"

req.query.shoe.color
// => "blue"

req.query.shoe.type
// => "converse"

Reference

Upvotes: 1

Related Questions