纯属分享
app.js
var express = require('express'); var path = require('path'); var app = express(); var port = process.env.port||8000; var wx = require('./admin/wx/wx.js'); //token认证--get请求 app.get('/wx',wx.token);
wx.js
var config = require('./config/config.js'); var API = require('wechat-api'); var url = require("url"); var crypto = require("crypto"); function wx(){ //token认证 this.token = function(req,res){ function sha1(str){ var md5sum = crypto.createHash("sha1"); md5sum.update(str); str = md5sum.digest("hex"); return str; } var query = url.parse(req.url,true).query; var signature = query.signature; var echostr = query.echostr; var timestamp = query['timestamp']; var nonce = query.nonce; var oriArray = new Array(); oriArray[0] = nonce; oriArray[1] = timestamp; oriArray[2] = "weixin";//这里是你在微信开发者中心页面里填的token,而不是**** oriArray.sort(); var original = oriArray.join(''); var scyptoString = sha1(original); if(signature == scyptoString){ res.end(echostr); console.log("确认成功!"); }else { res.end("false"); console.log("失败!"); } } } module.exports = new wx();