-
Notifications
You must be signed in to change notification settings - Fork 125
/
server.js
119 lines (109 loc) · 3.89 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
115
116
117
118
119
// acts as a dev server and a dist server
// netlify does not use this
const projectPath = process.env.PROJECT_PATH;
const http = require('http');
const fs = require('fs');
const path = require('path');
const fnFile = (file) => {
const functionsPath = path.join(projectPath, 'dist', process.env.PROJECT_NAME || '', 'functions' );
const destFile = [process.env.PROJECT_NAME, file].filter(_ => _).join('--');
return path.join(functionsPath, destFile);
}
http.createServer(function (request, response) {
if (request.url.indexOf('/api/ids') !== -1) {
console.log('api request starting...', request.url);
const query = request.url.split('?')[1] || '';
if (!process.env.INLINE_API) {
require('child_process').exec(`node ${fnFile("ids.js")} '${query}'`, {}, function(e, output, err) {
console.info(err);
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(output);
});
} else {
const output = require('./src/api/ids.js').processRequest(query);
response.writeHead(200, { 'Content-Type': 'application/json' });
response.end(JSON.stringify(output));
}
return;
}
if (request.url.indexOf('/api/export') !== -1) {
console.log('api request starting...', request.url);
const query = request.url.split('?')[1] || '';
if (!process.env.INLINE_API) {
require('child_process').exec(`node ${fnFile("export.js")} '${query}'`, {}, function(e, output, err) {
response.writeHead(200, {
'Content-Type': 'text/css',
'Content-Disposition': 'attachment; filename=interactive-landscape.csv'
});
response.end(output);
});
} else {
const output = require('./src/api/export.js').processRequest(query);
response.writeHead(200, {
'Content-Type': 'text/css',
'Content-Disposition': 'attachment; filename=interactive-landscape.csv'
});
response.end(output);
}
return;
}
let filePath = path.join(process.env.PROJECT_PATH, 'dist', request.url.split('?')[0]);
if (fs.existsSync(path.resolve(filePath, 'index.html'))) {
filePath = path.resolve(filePath, 'index.html');
} else if (fs.existsSync(filePath + '.html')) {
filePath = filePath + '.html'
}
const extname = path.extname(filePath);
let encoding = 'utf-8';
var contentType = 'text/html; charset=utf-8';
switch (extname) {
case '.js':
contentType = 'text/javascript; charset=utf-8';
break;
case '.css':
contentType = 'text/css; charset=utf-8';
break;
case '.json':
contentType = 'application/json; charset=utf-8';
break;
case '.svg':
contentType = 'image/svg+xml; charset=utf-8';
break;
case '.jpg':
contentType = 'image/jpg';
encoding = undefined;
break;
case '.png':
contentType = 'image/png';
encoding = undefined;
break;
case '.pdf':
contentType = 'application/pdf';
encoding = undefined;
break;
}
fs.readFile(filePath, encoding, function(error, content) {
if (error) {
const extraPath = filePath + '.html';
fs.readFile(extraPath, encoding, function(error, content) {
if (error) {
if(error.code == 'ENOENT') {
response.writeHead(200, { 'Content-Type': contentType });
response.end('404. Not found. ', 'utf-8');
} else {
response.writeHead(500);
response.end('Sorry, check with the site admin for error: '+error.code+' ..\n');
response.end();
}
} else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
} else {
response.writeHead(200, { 'Content-Type': contentType });
response.end(content, 'utf-8');
}
});
}).listen(process.env.PORT || 8001);
console.log(`Development server running at http://127.0.0.1:${process.env.PORT || 8001}/`);