-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
51 lines (40 loc) · 1.13 KB
/
server.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
43
44
45
46
47
48
49
50
51
'use strict';
const express = require('express');
const bodyParser = require('body-parser');
const errors = require('http-errors');
const searchVideo = require('./searchVideo');
const PORT = process.env.PORT || 3000;
const app = express();
// Middleware
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.post('/apm', (req, res, next) => {
const text = req.body.text;
if (!text) {
return next(new errors.BadRequest('Missing or invalid text parameter'));
}
const query = 'apm ' + text;
searchVideo(query)
.then((videoUrl) => {
res.json({
response_type: 'in_channel',
text: videoUrl
});
})
.catch(next);
});
app.use((error, req, res, next) => {
let status;
let message;
if (error.response) {
status = error.response.status;
message = error.response.statusText;
} else {
status = error.status || 500;
message = error.message;
}
res.status(status).json({ error: message });
});
app.listen(PORT, () => {
console.log('App listening on port', PORT);
});