http.get("http://www.google.com/index.html", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
我在文档上看到的例子。 微信平台验证的时候他们会把发一条GET消息,内容大概是
/wechat?signature=d34f1983fb3ecf933b9435ad5c05b6e3a138a0e5&echostr=5898562696761927225×tamp=1373464121&nonce=1373366264
但是我想自己本地模拟这个get发一下,验证我的服务器写得对不对,但是,不知道第一参数到底要怎么写? 比如写成 “http://127.0.0.1:80/wechat/?signature=d34f1983fb3ecf933b9435ad5c05b6e3a138a0e5&echostr=5898562696761927225×tamp=1373464121&nonce=1373366264” 但是好像有点问题。
8 回复
@ggaaooppeenngg 你這樣寫是沒錯的, 你說的有點問題是出了什麼問題? 是在parse query string方面嗎?
供你參考:
client.js
var http = require('http');
http.get("http://127.0.0.1:3000/wechat?signature=d34f1983fb3ecf933b9435ad5c05b6e3a138a0e5&echostr=5898562696761927225×tamp=137", function(res) {
console.log("Got response: " + res.statusCode);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
server.js
var http = require('http');
var url = require('url');
var qs = require('querystring');
http.createServer(function(req, res) {
res.writeHead(200, {"Content-Type": "text/plain"});
var query = url.parse(req.url).query;
res.end(JSON.stringify(qs.parse(query)));
}).listen(3000);
<1>好像是我的URL太长了写在了两行里面,我用sublime编辑,只有第一行被解析了所以说是非法的,我怎么把URL写在两行里面呀?
<2> 我的server用了express,大概是这样的
//app.js
app.get('/wechat',routes.token);
//index.js
exports.token = function (req, res) {
//console.log('Token~:')
//加密/校验流程:
//1. 将token、timestamp、nonce三个参数进行字典序排序
//2. 将三个参数字符串拼接成一个字符串进行sha1加密
//3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信
var arr = new Array(3);
arr[0] = '19931001'; // token
arr[1] = req.query.nonce;
arr[2] = req.query.timestamp;
console.log('nonce',req.query.nonce);
console.log('timestamp',req.query.timestamp);
console.log('echostr',req.query.echostr);
arr.sort();
var shasum = crypto.createHash('sha1');
shasum.update(arr.join(''));
if(shasum.digest('hex') == req.query.signature)
{
console.log('ok');
console.log(shasum.digest('hex'));
console.log(req.query.echostr);
res.send(req.query.echostr);
}
else
{
console.log(shasum.digest('hex'));
//console.log(req.query.signature);
res.send(req.query.signature);
}
};
F:\mynode\serverTest>node app
Express server listening on port 80
nonce 324234324
timestamp 137
echostr 5898562696761927225
Error: Not initialized
at Hash.digest (crypto.js:216:24)
at exports.token (F:\mynode\serverTest\routes\index.js:30:24)
at callbacks (F:\mynode\serverTest\node_modules\express\lib\router\index.js:
161:37)
at param (F:\mynode\serverTest\node_modules\express\lib\router\index.js:135:
11)
at pass (F:\mynode\serverTest\node_modules\express\lib\router\index.js:142:5
)
at Router._dispatch (F:\mynode\serverTest\node_modules\express\lib\router\in
dex.js:170:5)
at Object.router (F:\mynode\serverTest\node_modules\express\lib\router\index
.js:33:10)
at next (F:\mynode\serverTest\node_modules\express\node_modules\connect\lib\
proto.js:190:15)
at Object.methodOverride [as handle] (F:\mynode\serverTest\node_modules\expr
ess\node_modules\connect\lib\middleware\methodOverride.js:49:5)
at next (F:\mynode\serverTest\node_modules\express\node_modules\connect\lib\
proto.js:190:15)
GET /wechat?signature=d34f1983fb3ecf933b9435ad5c05b6e3a138a0e5&echostr=589856269
6761927225×tamp=137 500 32ms
好像我的crypto没有用还是怎么了这是为什么?
第一個問題:
在JavaScript你可以透過像這樣使用多行字串
var str = "abcdefghijkl\
mnopqrstuv"
** 第二個問題:**
因為你hasum.digest('hex')
用了兩次。
http://nodejs.org/api/crypto.html#crypto_hash_digest_encoding
Note: hash object can not be used after digest() method has been called.