Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

-

  • Loading branch information...
commit aa873a7503e010d453249f45ca57365ecb0eddf4 1 parent 87571d6
Joe Hitchens authored
Showing with 106 additions and 0 deletions.
  1. +21 −0 LICENSE
  2. +47 −0 jsond.js
  3. +8 −0 package.json
  4. +21 −0 test.html
  5. +9 −0 test.js
21 LICENSE
View
@@ -0,0 +1,21 @@
+
+Copyright 2011 Sleepless Software Inc. All rights reserved.
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to
+deal in the Software without restriction, including without limitation the
+rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
+sell copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+IN THE SOFTWARE.
+
47 jsond.js
View
@@ -0,0 +1,47 @@
+
+var http = require("http")
+
+var fail = function(res, err) {
+ var jsonOut = JSON.stringify({error:err.message+"\n"})
+ res.writeHead(500, {
+ "Content-Type": "text/plain",
+ "Content-Length": jsonOut.length,
+ })
+ res.end(jsonOut)
+}
+
+exports.createServer = function(msgHandler) {
+
+ return http.createServer(function(req, res) {
+ var jsonIn = ""
+ req.setEncoding("utf8")
+ if(req.method != "POST") {
+ fail(res, new Error("BAD METHOD: "+req.method))
+ }
+ else {
+ req.on("data", function(d) {
+ jsonIn += d
+ })
+ req.on("error", function(e) {
+ fail(res, e)
+ })
+ req.on("end", function() {
+ try {
+ var msgIn = JSON.parse(jsonIn)
+ msgHandler(jsonIn, function(msg) {
+ var jsonOut = JSON.stringify(msg)
+ res.writeHead(200, {
+ "Content-Type": "text/plain",
+ "Content-Length": jsonOut.length,
+ })
+ res.end(jsonOut)
+ })
+ }
+ catch(e) {
+ fail(res, e)
+ }
+ })
+ }
+ })
+}
+
8 package.json
View
@@ -0,0 +1,8 @@
+{"name":"jsond"
+,"version":"1.0.0"
+,"description":"Implements a simple server for sending and receiving JSON messages over HTTP"
+,"author":"Joe Hitchens <joe@sleepless.com>"
+,"license":{"type":"MIT" ,"url":"https://github.com/sleeplessinc/node-jsond/raw/master/LICENSE"}
+,"main":"jsond.js"
+,"repositories":[{"type":"git","url":"https://sleeplessinc@github.com/sleeplessinc/node-jsond.git"}]
+}
21 test.html
View
@@ -0,0 +1,21 @@
+<html>
+<body>
+
+Send: <input onchange="changed(this.value)"><p>
+Received: <textarea id=rcvd></textarea><p>
+
+<script>
+
+ function changed(v) {
+ var r = new XMLHttpRequest();
+ r.open("POST", "http://127.0.0.1:3333/", true);
+ r.onreadystatechange = function() {
+ if(r.readyState == 4) {
+ document.getElementById("rcvd").value = JSON.parse(r.responseText).r
+ r.onreadystatechange = function() {}
+ }
+ }
+ r.send(JSON.stringify({m:v}));
+ }
+
+</script>
9 test.js
View
@@ -0,0 +1,9 @@
+
+var jsond = require("./jsond")
+
+function msgHandler(msg, cb) {
+ cb({r:msg.m});
+}
+
+jsond.createServer(msgHandler).listen(3333)
+
Please sign in to comment.
Something went wrong with that request. Please try again.