简体   繁体   中英

Node - taking a URL and reformatting it as a POST

I'm trying to set my node server up to receive an encoded URL like this:

http://www.domain.com/room=office&light=light2&state=on

and then send it as a POST with the workload appropriately formatted as:

{room:'office', light:'light2', state: 'on'}

So far, I've parsed it so that it formats correctly:

 var postrelay=S(pathname).replaceAll('/', '{').s;
 var postrelay=S(postrelay).replaceAll('=', ':\'').s;
 var postrelay=S(postrelay).replaceAll('&', '\',').s;
 var postrelay= (postrelay + '\'}')  ;  

http.post('http://www.postdomain.com/', postrelay);

Can someone tell me what I need to change to make this work? The console log shows my parsed json string to be valid, but it isn't working.

You need to include a question mark at the beginning of the query string:

http://www.domain.com/?room=office&light=light2&state=on
                      ^

Once you do that, just parse the URL with url.parse and pass in the resulting object:

> var parsed = url.parse('http://www.domain.com/?room=office&light=light2&state=on', true);
> parsed.query
{ room: 'office',
  light: 'light2',
  state: 'on' }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM