Last active
May 4, 2018 05:26
-
-
Save dzabrzenski/db6081574d1108233b19728a881d0e54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
const http = require('http'); | |
const url = require('url'); | |
const querystring = require('querystring'); | |
const token = 'secret'; // replace to process.env.TOKEN for non development | |
const server = http | |
.createServer((req, res) => { | |
const parsed = url.parse(req.url); | |
req.url = parsed.pathname; | |
req.query = querystring.parse(parsed.query); | |
// check token in every request | |
if (req.query.token !== token) { | |
res.writeHead(401); | |
return res.end(); | |
} | |
// verification request | |
if (req.method === 'GET') { | |
return res.end(req.query.challenge); | |
} | |
req.rawBody = ''; | |
req.on('data', chunk => { | |
req.rawBody += chunk; | |
}); | |
req.on('end', () => { | |
if (req.rawBody) { | |
req.body = JSON.parse(req.rawBody); | |
} | |
handler(req, res); | |
}); | |
}); | |
function handler(req, res) { | |
// it will contain query response | |
console.log(req.body); | |
const data = { | |
parameters: { | |
name: 'John', | |
surname: 'Example', | |
age: '30' | |
}, | |
responses: [ | |
{ | |
type: 'text', | |
elements: ['Hi {{name}} {{surname}}'] | |
} | |
] | |
}; | |
res.end(JSON.stringify(data)); | |
} | |
server.listen(3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment