-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
114 lines (103 loc) · 2.45 KB
/
index.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 bodyParser from 'body-parser';
import fs from 'fs';
import http from 'http';
import path from 'path';
import webpack from 'webpack';
const __dirname = path.resolve();
const compiler = webpack({
mode: 'development',
devtool: 'inline-source-map',
watch: true,
entry: {
index: './src/index.js',
test: './src/test.js',
},
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js',
},
stats: 'errors-only',
module: {
rules: [
{
test: /\.(glsl|frag|vert)$/,
loader: 'raw-loader',
exclude: /node_modules/,
},
{
test: /\.(glsl|frag|vert)$/,
loader: 'glslify-loader',
exclude: /node_modules/,
},
{ test: /node_modules/, loader: 'ify-loader' },
{ test: /\.json$/, loader: 'json-loader' },
],
},
});
// eslint-disable-next-line no-unused-vars
const _ = compiler.watch(
{
aggregateTimeout: 300,
poll: undefined,
},
(err, stats) => {
console.log(stats);
}
);
const app = express();
const mimetype = {
'.html': 'text/html',
'.js': 'text/javascript',
'.css': 'text/css',
'.txt': 'text/plain',
'.jpg': 'image/jpeg',
'.gif': 'image/gif',
'.bmp': 'image/bmp',
'.png': 'image/png',
'.svg': 'image/svg+xml',
'.ico': 'image/x-icon',
'.ttf': 'application/x-font-ttf',
'.woff': 'application/x-font-woff',
'.woff2': 'text/plain',
'.fon': 'application/octet-stream',
'.ogv': 'application/ogg',
'.wav': 'audio/mpeg',
'.mp3': 'audio/mpeg',
'.ogg': 'audio/ogg',
};
function open(resp, urlA) {
const url = urlA.replace(/^.*(\..*?)$/, '$1') === urlA ? `${urlA}.js` : urlA;
const ext = url.replace(/^.*(\..+?)$/, '$1');
const mime = mimetype[ext] || 'text/html';
fs.readFile(url, (error, data) => {
resp.writeHead(200, {
'Content-Type': mime,
});
console.log(url);
resp.end(data);
});
}
app.use(
bodyParser.urlencoded({
extended: true,
})
);
app.use(bodyParser.json());
process.env.TZ = 'Europe/Amsterdam';
app.get('/test', (req, resp) => {
open(resp, 'public/test.html');
});
app.get('/', (req, resp) => {
open(resp, 'public/index.html');
});
app.get(/^\/(dist|public|src)\/.+$/, (req, resp) => {
const url = req.url.substring(1);
open(resp, url);
});
app.get('/*', (req, resp) => {
const url = `dist/${req.url.substring(1)}`;
open(resp, url);
});
const httpServer = http.createServer(app);
httpServer.listen(process.env.PORT || 8000);