Skip to content

server http

Emiel Mols edited this page Aug 12, 2015 · 1 revision

Incoming and outgoing http request.

Example:

server.coffee
	Http = require 'http'
	Event = require 'event'

	exports.client_fetchXKCD = ->
		Http = require 'http'
		Http.get
			url: 'http://xkcd.com'
			name: 'xkcdResponse' # corresponds to exports.xkcdResponse below

	exports.xkcdResponse = (data) !->
		# called when the Http API has the result for the above request
		log 'got some http response'
		Event = require 'event'
		Event.create
			text: "Fetched some data for you"
		
		re = /imgs.xkcd.com\/comics\/.*\.png/
		# regex to find the comic in html
		url = "http://" + re.exec(data)[0]
		Db.shared.set 'xkcdUrl', url

client.coffee:
	Db = require 'db'
	Dom = require 'dom'
	Ui = require 'ui'

	Dom.section !->
		Dom.text "API to make HTTP requests from the Happening backend."
		Ui.button "Fetch Latest XKCD", !->
			Server.send 'fetchXKCD'

		Dom.div !->
			if Db.shared.get('xkcdUrl')
				Dom.img !->
					Dom.prop 'src', Db.shared.get('xkcdUrl')
					Dom.style 'max-width': '100%'
			else 
				Dom.text "Nothing received yet"

Functions

request(opts)

Perform a http request.

opts is an object with the following keys:

  • url: The URL to request.
  • data: Data to provide with the POST (or GET?) request.
  • name: The name of the function that will be called with the response. Function should be exposed.
  • args: Arguments that should be returned to the name method.
  • follow: Boolean value, whether redirects should be followed.
  • asRaw: Boolean. If true, the raw CURL response will be returned.

For a simple request, you can just provide a url as string as the only argument, instead of opts.

post(opts), get(opts), put(opts)

Same as request, with given post addresses.

Events

onHttp(request)

Used to accept a http request from outside the system. For example: an other server or browser.

Example:

server.coffee:

	exports.onHttp = (request) ->
		# special entrypoint for the Http API: called whenever a request is made to our plugin's inbound URL
		log received:
		log request.data
		request.respond 200, "Thanks for your input\n"

Ironically, onHttp is not part of the http library, so no need to require anything.

Basic topics

API reference

  • API Reference
    • Client
      • [client plugin](client plugin)
      • [client dom](client dom)
      • [client obs](client obs)
      • [client db](client db)
      • [client server](client server)
      • [client page](client page)
      • [client ui](client ui)
      • [client form](client form)
      • [client icon](client icon)
      • [client modal](client modal)
      • [client photo](client photo)
      • [client photoview](client photoview)
      • [client time](client time)
      • [client share](client share)
      • [client map](client map)
      • [client geoloc](client geoloc)
    • Server
      • [server event](server event)
      • [server plugin](server plugin)
      • [server http](server http)
      • [server db](server db)
      • [server photo](server photo)
      • [server time](server time)
  • Example UI elements

Advanced topics

Clone this wiki locally