-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
114 lines (91 loc) · 2.58 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import Express from 'express';
import GraphHTTP from 'express-graphql';
import Schema from './graphql_connector';
import jwt from 'jsonwebtoken';
const path = require('path');
const multer = require('multer');
const cors = require('cors');
const engine = require('./engine');
const compression = require('compression')
const upload = multer({ dest: 'tmp/' });
const cluster = require('cluster');
const numCPUs = require('os').cpus().length;
if (cluster.isMaster) {
console.log(`Master ${process.pid} is running`);
// Fork workers.
for (let i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died`);
});
} else {
// Config
const APP_PORT = 8881;
// Start
const app = Express();
const SECRET = 'aslkdjlkaj10830912039jlkoaiuwerasdjflkasd';
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'authorization,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
app.use(compression())
app.use(Express.static('public'))
const verifyuser = async (req) => {
const token = req.headers.authorization;
if(token){
//console.log(token);
try {
const { user } = await jwt.verify(token, SECRET);
req.user = user;
} catch (err) {
//console.log(err);
}
//console.log(req.user);
req.next();
} else {
req.next();
}
};
app.use(verifyuser);
/*
app.use('/graphql',cors(), GraphHTTP({
schema: Schema,
pretty: true,
graphiql: true,
context: {
SECRET,
user: req.user,
},
}));
*/
// GraphQL
app.use('/graphql',cors(), GraphHTTP(req => ({
schema: Schema,
pretty: true,
graphiql: true,
context: {
SECRET,
user: req.user,
},
})),
);
//File Upload
app.post('/uploadfile',cors(), upload.any(), engine.upload);
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '/public/index.html'));
});
// App Listenls
app.listen(APP_PORT, () => {
console.log(`App listening on port ${APP_PORT}`);
});
}