user:p1 IP::: ffff:127.0.0.1节点js中的URL

时间:2015-12-28 09:05:07

标签: node.js

在我的node.js项目的本地设置中,我有一个php页面test.php。现在,当我在浏览器中输入url http://localhost:8080/test.php时,我在控制台中收到以下错误。

[12/28/2015 12:40:15] user:p1 IP ::: ffff:127.0.0.1 URL:/test.ph 请求PHP后端(GET)时出错:错误:连接ECONNREFUSED。

我应该安装一些节点js的模块,还是我的ubuntu版本的问题?

这是整个代码:

var http = require("http"),
https = require("https"),
mustache = require("mustache"),
fs = require("fs"),
pg = require("pg"),
url = require("url"),
crypto = require("crypto"),
serveStatic = require("serve-static"),
docxtemplater = require("docxtemplater"),
querystring = require("querystring"),
callstream = require("./callstream");

pg.defaults.poolSize = 25;





function log(text, req, stateObj) {
  var u = (stateObj?stateObj.u:undefined),
  ip = (req?req.connection.remoteAddress:undefined),
  url = (req?req.url:undefined);

  var d = new Date();
  process.stderr.write("===============\n[" + (d.getMonth() + 1) + "/" + d.getDate() + "/" + d.getFullYear() + " " + d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds() + "] " 
    + "user: " + (u||"(none)") + " IP: " + (ip||"(none)") + " URL: " + (url||"")
    + "\n" + text + "\n===============\n\n"
  );
};




function validateToken(req) {
  var rawCookie = req.headers.cookie;
  if (!rawCookie) {return false;}
  if (!rawCookie.length) {return false;}

  var rawCookie = rawCookie.match(/t\=[A-Za-z0-9\+\/\=]+\.[A-Za-z0-9\+\/\=]+/);

  if (!rawCookie) {return false;}

  var parsedCookie = rawCookie[0].replace("t=", "").split(".");
  if (parsedCookie[1] !== (crypto.createHash("sha256").update("sECr6eTt" + parsedCookie[0]).digest("base64"))) {return false;}
  try {
    var parsedPayload = JSON.parse((new Buffer(parsedCookie[0], "base64")).toString());
  }
  catch (e) {
    return false;
  }
  if (!parsedPayload) {return false;}
  var u = parseInt(parsedPayload.u),
  m = (parseInt(parsedPayload.m)||0),
  a = (parseInt(parsedPayload.a)||0);
  if (!u) {return false;}
  //if (!m) {return false;}
  //if (!a) {return false;}
  if ((new Date(parseInt(parsedPayload.t))).getDate() !== (new Date()).getDate()) {return false;}
  if (parsedPayload.s !== crypto.createHash("sha256").update("true" + req.connection.remoteAddress + "s4CcrEEtT").digest("hex")) {return false;}
  return {u:u, m:m, a:a};
};


function generateToken(req, u, m, a) {
  var u = parseInt(u),
  m = (parseInt(m)||0),
  a = (parseInt(a)||0);

  //if (!(u && m && a)) {log("token generation error");return "t=x";}

  var rawPayload = {u:u, m:m, a:a, t:Date.now(), s:(crypto.createHash("sha256").update("true" + req.connection.remoteAddress + "s4CcrEEtT").digest("hex"))};
  var encodedPayload = (new Buffer(JSON.stringify(rawPayload))).toString("base64");
  var sig = crypto.createHash("sha256").update("sECr6eTt" + encodedPayload).digest("base64");

  return encodedPayload + "." + sig;
};







function dbError(e, req, res, stateObj) {
  log("DB Error: " + e, req, stateObj);
  res.writeHead(500);
  res.end();
};


function noRoute(req, res, stateObj) {
  log("Bad Route: " + req.url, req, stateObj);
  res.writeHead(404);
  res.end();
};


function badParam(req, res, stateObj, pName, pVal) {
  log("Bad GET Param: " + pName + " = " + pVal, req, stateObj);
  res.writeHead(500);
  res.end();
};


var static = serveStatic("../html/static");

var staticPub = serveStatic("../html/pub");


function serve(req, res) {
  try {

  if (req.method !== "GET" && req.method !== "POST") {
    res.end();
    return;
  }

  var parsedUrl = url.parse(req.url, true),
  path = parsedUrl.pathname,
  queryParams = parsedUrl.query,
  stateObj = validateToken(req);


  if (path == "/favicon.ico") {
    res.writeHead(200, {'Content-Type': 'image/x-icon'});
    res.write(favicon);
    res.end();
    return;
  }

  if (stateObj) {
     // test.php url
       var cReq = http.request({hostname:"localhost", port:8080, path:parsedUrl.path}, function(cRes) {
          res.writeHead(200, cRes.headers);
          cRes.pipe(res);
        });
        cReq.on("error", function(e) {
          log("Error requesting PHP backend (GET): " + e, req, stateObj);
          res.writeHead(500);
          res.end();
        });
        cReq.end();

  }
  }
  catch (e) {
    log("Exception in primary request callback: " + e + "\n" + e.stack, req, stateObj)
    res.writeHead(500);
    res.end();
    return;
  }
};

http.createServer(serve).listen(8080);

0 个答案:

没有答案