-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
76 lines (62 loc) · 2.56 KB
/
Copy pathapp.js
File metadata and controls
76 lines (62 loc) · 2.56 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
const LiveDirectory = require('live-directory');
const HyperExpress = require('hyper-express');
const Socket = require('./models/Socket');
const Middlewares = require('./middlewares');
const Example = require('./controllers/Example');
const publicAssets = new LiveDirectory(`${__dirname}/public/`, {
cache: {
max_file_count: 1000,
max_file_size: 1024 * 1024 * 100,
},
});
const app = new HyperExpress.Server();
app.get('/public/*', (request, response) => {
// Strip away '/assets' from the request path to get asset relative path
// Lookup LiveFile instance from our LiveDirectory instance.
const path = request.path.replace('/public', '');
const file = publicAssets.get(path);
// Return a 404 if no asset/file exists on the derived path
if (file === undefined) return response.status(404).send();
const fileParts = file.path.split('.');
const extension = fileParts[fileParts.length - 1];
// Retrieve the file content and serve it depending on the type of content available for this file
const content = file.content;
if (content instanceof Buffer) {
// Set appropriate mime-type and serve file content Buffer as response body (This means that the file content was cached in memory)
return response.type(extension).send(content);
} else {
// Set the type and stream the content as the response body (This means that the file content was NOT cached in memory)
return response.type(extension).stream(content);
}
});
// Middleware to append trailing slash
app.use(Middlewares.slash);
// Setar CORS para evitar erro de cors origins caso carregue em outro dominio algo desse
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*'); // Substitua '*' pelo domínio específico em produção
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
// Responder a requisições de pré-voo (OPTIONS)
if (req.method === 'OPTIONS') {
res.status(200).send();
} else {
next();
}
});
app.get('/', Example.index);
//socket
app.get('/socket/', Example.socketExample);
app.post('/api/socket/execute/', Example.socketExecution);
//dropzone
app.get('/dropzone/', Example.dropzoneExample);
// Handle WebSocket connections
app.ws('/connect/', (connection) => {
// Parse the URL to get the query parameters
connection.setMaxListeners(0);
Socket.connect(connection);
});
app.listen(3000)
.then(() => {
console.log(`Webserver started on port 3000`);
})
.catch((code) => console.log(`Failed to start webserver on port 3000: ` + code));