-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (49 loc) · 1.09 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
var express = require( 'express' );
var app = express();
var type = process.argv[2];
var conf = require( './config.json' );
app.use( function( req, res, next ) {
var k, rule, redirect, reserved, displaced;
for ( k in conf.rewrites ) {
redirect = conf.rewrites[k];
try {
reserved = req.url.match( new RegExp( k ) );
displaced = redirect.match( /\$\d/g );
if ( !reserved ) throw '0';
if ( !displaced ) {
req.url = redirect;
next();
return;
}
else {
displaced.forEach( function( value ) {
var index = value.replace( '$', '' );
if ( reserved[ index ] ) {
redirect = redirect.replace( value, reserved[ index ] );
}
else {
throw '0';
}
});
req.url = redirect;
next();
return;
}
}
catch( e ) {
continue;
}
}
next();
});
if ( type == 'src' ) {
app.use( function( req, res, next ) {
res.header( 'Access-Control-Allow-Origin', '*' );
next();
});
app.use( express['static']( './src' ) );
}
else {
app.use( express['static']( './dist' ) );
}
app.listen( 80 );