nodejs-wechat
Advanced tools
Comparing version 0.0.1 to 0.0.2
var Wechat = require('./lib/wechat.js'); | ||
module.exports = Wechat; | ||
module.exports = Wechat; |
@@ -26,3 +26,4 @@ var events = require('events'); | ||
if (!this.verifyRequest(req)) { | ||
res.end(400); | ||
res.statusCode = 400; | ||
res.end(); | ||
return; | ||
@@ -48,4 +49,5 @@ } | ||
var xml = template.apply(outgoingMessage); | ||
session.res.type('xml'); | ||
session.res.send(xml); | ||
session.res.setHeader('Content-Type', 'application/xml'); | ||
session.res.write(xml); | ||
session.res.end(); | ||
} | ||
@@ -63,4 +65,7 @@ | ||
var signature = util.signature(this.token, timestamp, nonce); | ||
if (signature == req.query.signature)) { | ||
if (res) res.send(req.query.echostr); | ||
if (signature == req.query.signature) { | ||
if (res) { | ||
res.write(req.query.echostr); | ||
res.end(); | ||
} | ||
return true; | ||
@@ -73,4 +78,4 @@ } else { | ||
module.exports = Wechat; | ||
{ | ||
"name": "nodejs-wechat", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "wechat api", | ||
@@ -26,3 +26,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"require-all": "0.0.8" | ||
"require-all": "0.0.8", | ||
"mime": "~1.2.11" | ||
}, | ||
@@ -29,0 +30,0 @@ "devDependencies": { |
@@ -7,23 +7,59 @@ # Nodejs Wechat | ||
### Usage | ||
### __Usage__ | ||
#### Work with native http server | ||
```javascript | ||
var http = require('http'); | ||
var xmlBodyParser = require('express-xml-parser'); | ||
var Wechat = require('nodejs-wechat'); | ||
var opt = { | ||
token: token, | ||
url: '/weixin' | ||
token: 'TOKEN', | ||
url: '/' | ||
}; | ||
var wechatInstance = new Wechat(opt); | ||
var parse = xmlBodyParser(); | ||
var wechat = new Wechat(opt); | ||
wechat.on('event.subscribe', function(session) { | ||
session.replyTextMsg('欢迎您关注我们的订阅号'); | ||
}); | ||
var server = http.createServer(function(req, res) { | ||
parse(req, res, function(err) { | ||
if (err) { | ||
res.end(); | ||
return; | ||
} | ||
wechat.handleRequest(req, res); | ||
}); | ||
}); | ||
server.listen(80); | ||
``` | ||
#### Work with express | ||
```javascript | ||
var express = require('express'); | ||
var app = express(); | ||
var middlewares = require('express-middlewares-js'); | ||
var app = express(); | ||
app.use('/weixin', middlewares.xmlBodyParser()); | ||
app.use('/weixin', Wechat.bodyParser()); | ||
// alternative method | ||
// app.use('/weixin', middlewares.xmlBodyParser()); | ||
/* | ||
Alternative way | ||
app.get('/weixin', wechatInstance.verifyRequest.bind(wechatInstance)); | ||
app.post('/weixin', wechatInstance.handleRequest.bind(wechatInstance)); | ||
var xmlBodyParser = require('express-xml-parser'); | ||
app.use('/weixin', xmlBodyParser({ | ||
type: ['application/xmlplus', 'xml'], | ||
limit: '1mb' | ||
})); | ||
// you can work with other restful logics | ||
*/ | ||
var Wechat = require('nodejs-wechat'); | ||
var opt = { | ||
token: token, | ||
url: '/weixin' | ||
}; | ||
var wechat = new Wechat(opt); | ||
app.get('/weixin', wechat.verifyRequest.bind(wechat)); | ||
app.post('/weixin', wechat.handleRequest.bind(wechat)); | ||
// you can also work with other restful routes | ||
app.use('/api', middlewares.bodyParser()); | ||
@@ -52,5 +88,7 @@ | ||
}); | ||
app.listen(80); | ||
``` | ||
### Api | ||
### __API__ | ||
@@ -76,2 +114,25 @@ - `#verifyRequest(req, res)` | ||
#### Session | ||
- `incomingMessage` | ||
> This is a direct parse of weixin server request | ||
```xml | ||
<xml> | ||
<ToUserName><![CDATA[toUser]]></ToUserName> | ||
<FromUserName><![CDATA[FromUser]]></FromUserName> | ||
<CreateTime>123456789</CreateTime> | ||
<MsgType><![CDATA[event]]></MsgType> | ||
<Event><![CDATA[subscribe]]></Event> | ||
</xml> | ||
``` | ||
Becomes | ||
```json | ||
{ | ||
"ToUserName": "toUser", | ||
"FromUserName": "FromUser", | ||
"CreateTime": "123456789", | ||
"MsgType": "event", | ||
"Event": "subscribe" | ||
} | ||
``` | ||
- `req` | ||
@@ -94,1 +155,2 @@ > This is the request from weixin server | ||
- Advanced interfaces | ||
> Will finish advanced interfaces before July/2014, welcome send pull requests :) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12578
257
153
2
+ Addedmime@~1.2.11
+ Addedmime@1.2.11(transitive)