fixed null body for get headers
[henge/kiak.git] / main.js
diff --git a/main.js b/main.js
index 75e762f..f123a41 100644 (file)
--- a/main.js
+++ b/main.js
@@ -25,6 +25,16 @@ const router = {
   httpd:      undefined,
   wsProtocol: opts['no-tls'] ? 'ws' : 'wss',
   respond:    (request,response) => {
+    let body = []
+    request.on('error', function(err) {
+      console.error(`error is ${err}`);
+    }).on('data', function(chunk) {
+      console.log(`chunk is ${chunk}`)
+      body.push(chunk);
+    }).on('end', function() {
+      console.log(`body is ${body}`)
+    })
+    console.log('server handling request')
     const serveFile = (fPath) => {
       fs.readFile(fPath, { encoding: 'utf8' }, (err, data) => {
         if (err || data == undefined) {
@@ -42,7 +52,7 @@ const router = {
     let routePath = htArgv[0].split('/')
     let routeName = routePath[0]
     if (routeName === '' || routeName === 'index.html')
-    serveFile(opts['index'])
+      serveFile(opts['index'])
     else if (routeName in opts['bindings']) {
       let localPath = path.normalize(opts['bindings'][routeName].concat(path.sep + routePath.slice(1).join(path.sep)))
       if (localPath.includes(opts['bindings'][routeName])) {
@@ -57,14 +67,27 @@ const router = {
         console.log(`SEC: ${localPath} references files not in route`)
       }
     }
+    /* TODO: Handle reconnecting host */
     else if (routeName in router.routes) {
+
       const route = router.routes[routeName]
-      response.writeHead(200, { 'Content-Type': 'text/html' })
-      response.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`)
-      response.end()
-      //TODO: if route.socket == undefined: have server delay this send until host connects
-      //      (this happens when a client connects to an active route with no currently-online host)
-      route.socket.send(request.headers['x-forwarded-for'] || request.connection.remoteAddress)
+
+      /* Client is INIT GET */
+      if (request.headers['x-strapp-type'] == undefined) {
+        console.log('client init GET')
+        response.writeHead(200, { 'Content-Type': 'text/html' })
+        response.write(`${router.skelPage[0]}${router.clientJS}${router.skelPage[1]}`)
+        response.end()
+        //TODO: if route.socket == undefined: have server delay this send until host connects
+        //      (this happens when a client connects to an active route with no currently-online host)
+      }
+      else { /* Client sent offer, waiting for answer */
+        console.log(JSON.parse(request.headers['x-strapp-type']))
+        route.socket.on('message', (hostResponse) => {
+          console.log(hostResponse)
+        })
+      }
+
     }
     else {
       router.routes[routeName] = true
@@ -101,6 +124,7 @@ const router = {
   * @summary Boot up the router.  With TLS, we must wait for file reads to sync.
   */
   if (!opts['no-tls']) {
+    console.log('tls')
     let filesRead = 0
     let key = undefined
     let cert = undefined