visualvault-api 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,128 @@
1
+
2
+ var logger = require('./log');
3
+
4
+ var message = "Web site starting";
5
+ logger.info(message);
6
+
7
+ //If using a Virtual Directory name, set routeAlias
8
+ //For example, https://myserver:3000/vvnodeserver
9
+ //If virtual directory is not present in the URL it will be ignored
10
+ var routeAlias = 'vvnodeserver';
11
+
12
+ var express = require('express'),
13
+ scriptController = require('./routes/scripts'),
14
+ scheduleController = require('./routes/scheduledscripts'),
15
+ testScheduledScriptsController = require('./routes/testScheduledScripts'),
16
+ http = require('http'),
17
+ https = require('https'),
18
+ fs = require('fs'),
19
+ path = require('path'),
20
+ bodyParser = require('body-parser'),
21
+ multer = require('multer'),
22
+ favicon = require('serve-favicon'),
23
+ expressErrorHandler = require('express-error-handler');
24
+
25
+ var app = express();
26
+
27
+ var upload=multer();
28
+
29
+
30
+ app.set('port', process.env.PORT || 3000);
31
+ app.set('httpsPort', process.env.PORT || 3001);
32
+ app.set('views', __dirname + '/views');
33
+ app.set('view engine', 'ejs');
34
+ app.use(allowCors);
35
+ app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
36
+ //app.use(bodyParser.json());
37
+ app.use(bodyParser.json({limit: '5mb'}));
38
+ app.use(express.static(path.join(__dirname, 'public')));
39
+ app.use(errorHandler);
40
+
41
+
42
+ //express error handler
43
+ function errorHandler(err, req, res, next) {
44
+
45
+ //if http headers sent do not attempt to send a response
46
+ //next(err) will pass the error to default express error handler
47
+ if (res.headersSent) {
48
+ return next(err);
49
+ }
50
+
51
+ logger.error(err.stack);
52
+ res.status(500);
53
+ res.render('error', { error: err });
54
+ }
55
+
56
+ // Add CORS headers
57
+ function allowCors(req, res, next) {
58
+
59
+ // Website you wish to allow to connect
60
+ res.setHeader('Access-Control-Allow-Origin', '*');
61
+
62
+ // Request methods you wish to allow
63
+ res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
64
+
65
+ // Request headers you wish to allow
66
+ res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
67
+
68
+ // Set to true if you need the website to include cookies in the requests sent
69
+ // to the API (e.g. in case you use sessions)
70
+ res.setHeader('Access-Control-Allow-Credentials', true);
71
+
72
+ if (req.method === "OPTIONS") {
73
+ res.header('Access-Control-Allow-Origin', req.headers.origin);
74
+ console.log("OPTIONS Requested");
75
+ } else {
76
+ res.header('Access-Control-Allow-Origin', '*');
77
+ }
78
+
79
+ // Pass to next layer of middleware
80
+ next();
81
+ }
82
+
83
+
84
+ //HTTP GET: /
85
+ app.get('/' + routeAlias, scriptController.testsite);
86
+ app.get('/node/' + routeAlias, scriptController.testsite);
87
+ app.get('/', scriptController.testsite);
88
+ app.get('/node/', scriptController.testsite);
89
+
90
+ //HTTP GET: /app.js
91
+ app.get('/' + routeAlias + '/app.js', scriptController.testsite);
92
+ app.get('/node/' + routeAlias + '/app.js', scriptController.testsite);
93
+ app.get('/app.js', scriptController.testsite);
94
+ app.get('/node/app.js', scriptController.testsite);
95
+
96
+ //HTTP POST: /scripts
97
+ //HTTP POST: /node/scripts
98
+ app.post('/' + routeAlias + '/scripts', scriptController.scripts);
99
+ app.post('/node/' + routeAlias + '/scripts', scriptController.scripts);
100
+ app.post('/scripts', scriptController.scripts);
101
+ app.post('/node/scripts', scriptController.scripts);
102
+
103
+ //HTTP GET: /scripts
104
+ //HTTP GET: /node/scripts
105
+ app.get('/' + routeAlias + '/scripts', scriptController.scripts);
106
+ app.get('/node/' + routeAlias + '/scripts', scriptController.scripts);
107
+ app.get('/scripts', scriptController.scripts);
108
+ app.get('/node/scripts', scriptController.scripts);
109
+
110
+ //HTTP POST: /scheduledscripts
111
+ //HTTP POST: /node/scheduledscripts
112
+ app.post('/' + routeAlias + '/scheduledscripts',upload.array(),scheduleController.processRequest);
113
+ app.post('/node/' + routeAlias + '/scheduledscripts',upload.array(),scheduleController.processRequest);
114
+ app.post('/scheduledscripts',upload.array(),scheduleController.processRequest);
115
+ app.post('/node/scheduledscripts',upload.array(),scheduleController.processRequest);
116
+
117
+ //HTTP GET: /testscripts/scheduled/:name
118
+ //HTTP GET: /node/testscripts/scheduled/:name
119
+ app.get('/' + routeAlias + '/testscripts/scheduled/:name', testScheduledScriptsController.processRequest);
120
+ app.get('/node/' + routeAlias + '/testscripts/scheduled/:name', testScheduledScriptsController.processRequest);
121
+ app.get('/testscripts/scheduled/:name', testScheduledScriptsController.processRequest);
122
+ app.get('/node/testscripts/scheduled/:name', testScheduledScriptsController.processRequest);
123
+
124
+ //Start listening on configured port
125
+ http.createServer(app).listen(app.get('port'), function () {
126
+ logger.info("Express server listening on port " + app.get('port'));
127
+ console.log("Express server listening on port " + app.get('port'));
128
+ });