forked from markmur/react-slack-feedback
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
48 lines (40 loc) · 1.27 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
const express = require('express');
const fs = require('fs');
const app = express();
const multer = require('multer');
const upload = multer({ dest: 'uploads/' });
const bodyParser = require('body-parser');
const fetch = require('isomorphic-fetch');
const cloudinary = require('cloudinary');
const config = require('./env');
cloudinary.config({
cloud_name: config.CLOUDINARY.CLOUD_NAME,
api_key: config.CLOUDINARY.API_KEY,
api_secret: config.CLOUDINARY.API_SECRET
});
var jsonParser = bodyParser.json();
// Send payload to Slack
app.post('/api/slack', jsonParser, (req, res) => {
fetch(config.WEBHOOK_URL, {
method: 'POST',
body: JSON.stringify(req.body)
})
.then(response => {
// NOTE not sending the full response back to the client because it contains
// the slack webhook url
return res.status(response.status).send({
status: response.status,
statusText: response.statusText
});
});
});
// Upload image to Cloudinary
app.post('/api/upload', upload.single('image'), (req, res) => {
var stream = cloudinary.uploader.upload_stream(result => {
fs.unlink(req.file.path);
return res.status(200).send(JSON.stringify(result.url));
});
var file_reader = fs.createReadStream(req.file.path).pipe(stream);
});
// Listen
app.listen(3001);