-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
56 lines (44 loc) · 1.28 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
52
53
54
55
56
require("dotenv").config();
var Twitter = require("twitter");
var express = require("express");
var path = require("path");
var client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
access_token_key: process.env.ACCESS_TOKEN,
access_token_secret: process.env.ACCESS_SECRET,
});
const app = express();
const log = console.log;
const PORT = process.env.PORT || 3000;
app.use(express.urlencoded({ extended: false }))
// parse application/json
app.use(express.json())
app.get("/", (req, res) => {
// Render HTML
res.sendFile(path.join(__dirname, './index.html'));
});
app.post("/twitter/message", async(req, res) => {
const tweet = req.body.tweet;
try {
const response = await postToTwitter(tweet);
res.json({
message: 'Tweet successfully posted to twitter'
});
} catch (error) {
res.status(500).json({
message: 'Not able to post'
});
}
});
function postToTwitter(tweet) {
client.post(
"statuses/update",
{ status: tweet },
function (error, tweet, response) {
if (error) log('Error occurs while posting to twitter');
log(tweet); // Tweet body.
}
);
}
app.listen(PORT, log('Server is starting at port ', PORT));