-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
37 lines (28 loc) · 1.02 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
'use strict'
const express = require('express');
const Bot = require('./bot');
const config = require('./config/index');
var app = express();
// DuerOS会定期发送探活请求到你的服务,确保你的服务正常运转
app.head('/', (req, res) => {
res.sendStatus(204);
});
// 监听post请求,DuerOS以http POST的方式来请求你的服务
app.post('/', (req, res) => {
req.rawBody = '';
console.log(new Date().toLocaleString());
req.setEncoding('utf-8');
req.on('data', function(chunk) {
req.rawBody += chunk;
});
req.on('end', function() {
var b = new Bot(JSON.parse(req.rawBody));
b.then(function(data){
res.send(data);
});
// 开启签名认证
// 为了避免你的服务被非法请求,建议你验证请求是否来自于DuerOS
// b.initCertificate(req.headers, req.rawBody).enableVerifyRequestSign();
});
}).listen(config.app_config.port);
console.log('DuerOS Services Listen On ' + config.app_config.port);