-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
65 lines (55 loc) · 1.47 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
57
58
59
60
61
62
63
64
65
require('babel-register');
const path = require('path');
const webpack = require('webpack');
const webpackDevMiddleware = require('webpack-dev-middleware');
const webpackHotMiddleware = require('webpack-hot-middleware');
const express = require('express');
const config = require('./webpack.config.babel.js');
const PORT = 8080;
const app = express();
const compiler = webpack(config);
const webpackMiddleware = webpackDevMiddleware(compiler, {
publicPath: config.output.publicPath,
contentBase: 'http://localhost:${PORT}/',
stats: {
colors: true,
hash: false,
timings: true,
chunks: false,
chunkModules: false,
modules: false,
},
});
app.use(webpackMiddleware);
app.use(webpackHotMiddleware(compiler));
const Twitter = require('twitter');
const client = new Twitter({
consumer_key: process.env.CONSUMER_KEY,
consumer_secret: process.env.CONSUMER_SECRET,
bearer_token: process.env.BEARER_TOKEN,
});
app.get('/api/twitter/search/tweets.json', (req, res) => {
client.get('search/tweets', { q: req.query.q }, (error, tweets, response) => {
if (error) {
res
.status(500)
.json({
error,
message: 'Twitter API responded with error',
});
}
else {
res.json(tweets);
}
});
});
app.get('*', (req, res) => {
res.write(webpackMiddleware.fileSystem.readFileSync(path.join(__dirname, 'dist/index.html')));
res.end();
});
app.listen(PORT, (err) => {
if (err) {
return console.log(err);
}
console.log(`Listening at http://localhost:${PORT}/`);
});