-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
116 lines (92 loc) · 2.89 KB
/
index.js
File metadata and controls
116 lines (92 loc) · 2.89 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
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
'use strict';
var chalk = require('chalk');
var express = require('express');
var path = require('path');
var process = require('process');
var util = require('./util');
var fs = require('fs');
// setup credentials for cloud trace management
require('./DiagnosticsBuddy/index.js').enableAzureUploads();
console.log('process.env.DO_TTD_RECORD is [' + process.env.DO_TTD_RECORD + ']');
console.log('process.env.DIAGNOSTICS_BUDDY_STORAGE_CREDENTIALS is [' + process.env.DIAGNOSTICS_BUDDY_STORAGE_CREDENTIALS + ']');
console.log('path.resolve(\'./\' is [' + path.resolve('./') + ']')
// set tracing options for demo purposes
if (process.jsEngine && process.jsEngine === 'chakracore') {
var trace_mgr = require('trace_mgr');
trace_mgr.setOptions({
initialRates: {
emitOnLogWarn: 1.0,
emitOnLogError: 1.0,
emitOnAssert: 1.0,
localTraceDirectory: __dirname
}
});
}
if (!fs.existsSync('_tmptmptmp')) {
fs.mkdirSync('_tmptmptmp');
}
if (!fs.existsSync('_diagnosticTraces')) {
fs.mkdirSync('_diagnosticTraces');
}
const DATA_DIR = path.resolve(__dirname, 'testdata');
var app = express();
app.set('view engine', 'ejs');
app.set('views', path.resolve(__dirname, 'views'));
function noCache(req, res, next) {
res.setHeader('Cache-Control', 'no-cache');
next();
}
app.get('/', noCache, function (req, res) {
console.log(`${new Date().toISOString()}: new connection from ${req.ip.slice(req.ip.lastIndexOf(':') + 1)}`);
util.loadDirectoryInfo(DATA_DIR, req, res, 'index.ejs', DATA_DIR);
});
app.get('/forceTrace', noCache, function (req, res) {
process.nextTick(() => {
setTimeout(() => {
console.error('this is a forced trace!');
util.loadDirectoryInfo(DATA_DIR, req, res, 'index.ejs', DATA_DIR);
});
});
});
app.get('/subdir/', noCache, function (req, res) {
util.loadDirectoryInfo(DATA_DIR, req, res, 'dir.ejs');
});
app.get('/user/:userName/subdir/:subpath', noCache, function (req, res) {
util.loadDirectoryInfo(DATA_DIR, req, res, 'dir.ejs');
});
app.get('/user/:userName/contents/:subpath', noCache, function (req, res) {
util.loadFileInfo(DATA_DIR, req, res);
});
app.get('/user/:userName', noCache, function (req, res) {
util.loadDirectoryInfo(DATA_DIR, req, res, 'index.ejs');
});
app.use(function (err, req, res, next) {
console.error(err.stack);
res.status(500).send('Something broke!');
});
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
var server = app.listen(port, function () {
var addr = server.address();
var msg = {
engine: chalk.green(process.jsEngine ? process.jsEngine : 'v8'),
port: addr.port,
pid: process.pid
}
console.log(msg);
});