IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    nodejs https 请求 报错 Segmentation fault

    wei63w发表于 2017-06-04 05:25:48
    love 0

    如题所述, 这破问题卡了我好几天了已经, 求大神KO
    如果是在本地测 就会报错这个:Cannot GET /sns/jscode2session 如果我发布到服务器 就报错 : Segmentation fault 我贴本地代码:

    main.js

    var express = require('express');
    var app = express();
    var fs = require("fs");
    var http = require('http');
    var querystring = require('querystring'); 
    
    var test = require('./testrequest');
    var testfirst = app.get('/test/test',function(req,res){
    	var tt = test.test(req,function(reschild,data){
    		res.write(data);
    		res.write('\n\r');
    		res.end('test parent is show');
    	});
    });
    var server = app.listen(8081, function () {
    
      var host = server.address().address
      var port = server.address().port
    
      console.log("应用实例,访问地址为 http://%s:%s", host, port)
    
    });
    

    testrequest.js

    var querystring = require('querystring'); 
    var http = require('http');
    
    
    exports.test = function(parRes,data){
    	
    	var appid = 'wx2c0f777c4625ed86';
    	var secret = '5a47d4297972d7f4c84145e2ba92669f';
    	var code = parRes.query.code;
    	var newdata = {
    		appid:appid,
    		secret:secret,
    		code:code,
    		grant_type:'authorization_code'
    	}
    	var content = querystring.stringify(newdata);
    	var path = 'https://api.weixin.qq.com/sns/jscode2session?'+content;
    	console.log('path is :'+path	);
    	
    	const options = {
    	  hostname: '127.0.0.1',
    	  port: 8081,
    	  path: path,       //'/testget?'+content,
    	  method: 'get',
    	  headers: {
    	    'Content-Type': 'application/x-www-form-urlencoded',
    	    'Content-Length': Buffer.byteLength(content)
    	  }
    	};
    	
    	var req = http.request(options,(res) => {
    		  console.log(`STATUS: ${res.statusCode}`);
    		  console.log(`HEADERS: ${JSON.stringify(res.headers)}`);
    		  res.setEncoding('utf8');
    		  res.on('data', (chunk) => {
    		    console.log(`BODY: ${chunk}`);
    		    data('BODY',chunk);
    		  });
    		  res.on('end', (chunk) => {
    		    console.log('No more data in response.');
    		    
    		  });
    	});
    	req.on('error', (e) => {
    		data('error',e.message);
    	  console.error(`problem with request: ${e.message}`);
    	});
    	
    	// write data to request body
    	req.write(content);
    	req.end();
    }
    

    求大牛 指点



沪ICP备19023445号-2号
友情链接