Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
82 lines (67 loc) · 1.97 KB
/
Copy pathserver.js
File metadata and controls
82 lines (67 loc) · 1.97 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const express = require("express");
const MongoClient = require("mongodb").MongoClient;
const bodyParser = require("body-parser");
const app = express();
const port = 8001;
const dbUri = require("./config/db");
const uri = dbUri.uri;
const url = "mongodb://localhost:27017/";
const dbNameCloud = "test";
const dbNameLocal = "archive";
const http = require('http');
const sockjs = require('sockjs');
const cors = require('cors');
//server.listen(9999, '0.0.0.0');
//app.use(cors());
app.use((req, res, next) => {
res.header('Access-Control-Allow-Credentials', true);
res.header("Access-Control-Allow-Origin", "http://localhost:3000");
res.header(
"Access-Control-Allow-Headers",
"Origin, Credentials, X-Requested-With, Content-Type, Accept"
);
next();
});
app.use(bodyParser.json());
app.use(
bodyParser.urlencoded({
extended: true,
})
);
(async function () {
let clientCloud;
let clientLocal;
try {
clientCloud = await MongoClient.connect(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
clientLocal = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
console.log("Connected correctly to server");
const dbCloud = clientCloud.db(dbNameCloud);
require("./app/routes/cloud")(app, dbCloud);
const dbLocal = clientLocal.db(dbNameLocal);
require("./app/routes/local")(app, dbLocal);
const server = http.createServer(app);
const echo = sockjs.createServer({prefix:'/echo'});
echo.on('connection', (conn) => {
setInterval(() => {
// отправка времени
conn.write(new Date().toLocaleTimeString());
}, 1000)
conn.on('data', (message)=> {
conn.write(message);
});
conn.on('close', () => {});
});
echo.installHandlers(server, {prefix:'/echo'});
server.listen(port, () => {
console.log("server " + port);
});
} catch (err) {
console.log(err.stack);
}
})();