-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
42 lines (34 loc) · 994 Bytes
/
index.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
37
38
39
40
41
42
var express = require('express');
var app = express();
var url = require('url');
var request = require('request');
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.set('port', (process.env.PORT || 9001));
app.get('/', function(req, res){
res.send('It works!');
});
app.post('/post', function(req, res){
var parsed_url = url.format({
pathname: 'https://api.genius.com/search',
query: {
access_token: process.env.GENIUS_ACCESS,
q: req.body.text
}
});
request(parsed_url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var data = JSON.parse(body);
var first_url = data.response.hits[0].result.url;
var body = {
response_type: "in_channel",
text: first_url
};
res.send(body);
}
});
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});