main.js
function (obj, meta, status = 200, errorCode = -1, devMsg = null, usrMsg = null, moreInfo = null) {
      if (res.finished) {
        // request ended
        return
      }

      // ref.: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
      if (status < 300) {
        // OK
        // respond client and close request
        let body
        switch (typeof obj) {
          case 'object':
            // expected
            if (meta) {
              res.setHeader('X-Metadata', 'true')
              body = JSON.stringify({ 'meta': meta, 'result': obj })
            } else {
              body = JSON.stringify(obj)
            }
            break

          case 'string':
          case 'number':
            // maybe an ID returned after modification request
            body = JSON.stringify({ 'result': obj })
            break

          default:
            // return null JSON object
            body = '{}'
        }

        if (verb === 'GET') {
          // ETag to verify browser cache validation
          // have to be set here, web servers will not etag proxy requests
          // Cloudflare keep only weak ETags
          res.setHeader('ETag', etag(body, { 'weak': true }))
        }
        res.writeHead(status)
        res.end(body)
      } else if (status < 400 && typeof obj === 'string') {
        // redirect
        // expect obj to be the redirect URL
        // return object with previous and next (after redirect) URL
        let response = {
          'status': status,
          'requested_url': req.url,
          'endpoint': obj
        }
        // explain type of redirect in message
        if (status === 301) {
          response.message = 'Moved permanently, please re-send this request to the specified endpoint'
        } else {
          response.message = 'Temporary redirect, re-send this request to the specified temporary endpoint, ' +
            'continue to use the original request endpoint for future requests'
        }

        res.writeHead(status, { 'Location': obj })
        res.end(JSON.stringify(response))
      } else if (typeof obj === 'object' && obj !== null && Object.keys(obj).length > 0) {
        // custom error object
        res.writeHead(status)
        res.end(JSON.stringify(obj))
      } else {
        // client and/or server error
        httpErrorHandling(res, status, errorCode, devMsg, usrMsg, moreInfo)
      }
    }
function (obj, meta, status = 200, errorCode = -1, devMsg = null, usrMsg = null, moreInfo = null) {
      if (res.finished) {
        // request ended
        return
      }

      // ref.: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
      if (status < 300) {
        // OK
        // respond client and close request
        let body
        switch (typeof obj) {
          case 'object':
            // expected
            if (meta) {
              res.setHeader('X-Metadata', 'true')
              body = JSON.stringify({ 'meta': meta, 'result': obj })
            } else {
              body = JSON.stringify(obj)
            }
            break

          case 'string':
          case 'number':
            // maybe an ID returned after modification request
            body = JSON.stringify({ 'result': obj })
            break

          default:
            // return null JSON object
            body = '{}'
        }

        if (verb === 'GET') {
          // ETag to verify browser cache validation
          // have to be set here, web servers will not etag proxy requests
          // Cloudflare keep only weak ETags
          res.setHeader('ETag', etag(body, { 'weak': true }))
        }
        res.writeHead(status)
        res.end(body)
      } else if (status < 400 && typeof obj === 'string') {
        // redirect
        // expect obj to be the redirect URL
        // return object with previous and next (after redirect) URL
        let response = {
          'status': status,
          'requested_url': req.url,
          'endpoint': obj
        }
        // explain type of redirect in message
        if (status === 301) {
          response.message = 'Moved permanently, please re-send this request to the specified endpoint'
        } else {
          response.message = 'Temporary redirect, re-send this request to the specified temporary endpoint, ' +
            'continue to use the original request endpoint for future requests'
        }

        res.writeHead(status, { 'Location': obj })
        res.end(JSON.stringify(response))
      } else if (typeof obj === 'object' && obj !== null && Object.keys(obj).length > 0) {
        // custom error object
        res.writeHead(status)
        res.end(JSON.stringify(obj))
      } else {
        // client and/or server error
        httpErrorHandling(res, status, errorCode, devMsg, usrMsg, moreInfo)
      }
    }
function ( obj , meta , status = 200 , errorCode = - 1 , devMsg = null , usrMsg = null , moreInfo = null ) { if ( res . finished ) { // request ended return } // ref.: https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html if ( status < 300 ) { // OK // respond client and close request let body switch ( typeof obj ) { case 'object' : // expected if ( meta ) { res . setHeader ( 'X-Metadata' , 'true' ) body = JSON . stringify ( { 'meta' : meta , 'result' : obj } ) } else { body = JSON . stringify ( obj ) } break case 'string' : case 'number' : // maybe an ID returned after modification request body = JSON . stringify ( { 'result' : obj } ) break default : // return null JSON object body = '{}' } if ( verb === 'GET' ) { // ETag to verify browser cache validation // have to be set here, web servers will not etag proxy requests // Cloudflare keep only weak ETags res . setHeader ( 'ETag' , etag ( body , { 'weak' : true } ) ) } res . writeHead ( status ) res . end ( body ) } else if ( status < 400 && typeof obj === 'string' ) { // redirect // expect obj to be the redirect URL // return object with previous and next (after redirect) URL let response = { 'status' : status , 'requested_url' : req . url , 'endpoint' : obj } // explain type of redirect in message if ( status === 301 ) { response . message = 'Moved permanently, please re-send this request to the specified endpoint' } else { response . message = 'Temporary redirect, re-send this request to the specified temporary endpoint, ' + 'continue to use the original request endpoint for future requests' } res . writeHead ( status , { 'Location' : obj } ) res . end ( JSON . stringify ( response ) ) } else if ( typeof obj === 'object' && obj !== null && Object . keys ( obj ) . length > 0 ) { // custom error object res . writeHead ( status ) res . end ( JSON . stringify ( obj ) ) } else { // client and/or server error httpErrorHandling ( res , status , errorCode , devMsg , usrMsg , moreInfo ) } }
respond client and close request
respond client and close request