Book Image

JavaScript JSON Cookbook

By : Ray Rischpater, Brian Ritchie, Ray Rischpater
Book Image

JavaScript JSON Cookbook

By: Ray Rischpater, Brian Ritchie, Ray Rischpater

Overview of this book

Table of Contents (17 chapters)
JavaScript JSON Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Issuing a web service request using Node.js


So far, our server doesn't do much in response to a POST request; all it does is say "OK" and return the client's JSON back to the client. Typically, your server will need to do something with the JSON you provide, that is, make a web or database query, for example, or perform a computation. Our example queries the web service JSON endpoint at http://www.aprs.fi/, which lets you see how you can make a server-to-server web service request using Node.js.

Getting ready

If you want to run the example for yourself, you'll first need to go to http://www.aprs.fi, register for an account, and obtain an API key. Follow the links on the page to do this, and substitute your API key for the text "—key-"in the example that follows.

How to do it...

Our Node.js code will construct a URL with the identifier of the station we're interested in and our API key, and issue an additional HTTP request on behalf of the client. It looks like this:

var request = require('server...