-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (54 loc) · 2 KB
/
app.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
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
require('dotenv').config();
const indexRouter = require('./routes/index');
const featuresRouter = require('./routes/features');
const dashboardRouter = require('./routes/dashboard');
const cors = require('cors');
const nocache = require("nocache");
const compression = require("compression");
const helmet = require("helmet");
const app = express();
// Set up mongoose connection
const mongoose = require("mongoose");
mongoose.set('strictQuery', false);
const mongoDB = process.env.NODE_ENV === 'production' ? process.env.MONGODB_URI : process.env.DEV_DB_URL;
mongoose.connect(mongoDB, { useNewUrlParser: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on("error", console.error.bind(console, "MongoDB connection error:"));
const emitter = require('./twitter/twitterServer');
const debug = require("debug")("app");
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(compression());
app.use(express.static(path.join(__dirname, 'public')));
app.use(cors({ origin: `${process.env.NODE_ENV === 'development' ? 'http://localhost:5173' : 'https://www.power-map.io'}` }));
app.use(nocache());
app.use(helmet({
crossOriginEmbedderPolicy: false,
crossOriginResourcePolicy: false
}));
app.use('/', indexRouter);
app.use('/api/v1/features', featuresRouter);
app.use('/api/v1/dashboard', dashboardRouter);
// sse
app.get('/stream', (req, res, next) => {
const headers = {
'Content-Type': 'text/event-stream',
'Connection': 'keep-alive',
'Cache-Control': 'no-cache'
};
res.writeHead(200, headers);
res.write(`data: connected to sse\n\n`);
res.flush();
emitter.on('new-feature',() => {
debug(`A new-feature event has been detected.`);
res.write(`data: new feature added\n\n`);
res.flush();
});
});
module.exports = app;