-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
96 lines (86 loc) · 3.22 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
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* Zero to Blockchain */
var express = require('express');
var http = require('http');
var https = require('https');
var path = require('path');
var fs = require('fs');
var mime = require('mime');
var bodyParser = require('body-parser');
var cfenv = require('cfenv');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var vcapServices = require('vcap_services');
var uuid = require('uuid');
var env = require('./controller/envV2.json');
var sessionSecret = env.sessionSecret;
var appEnv = cfenv.getAppEnv();
var app = express();
var busboy = require('connect-busboy');
app.use(busboy());
// the session secret is a text string of arbitrary length which is
// used to encode and decode cookies sent between the browser and the server
/**
for information on how to enable https support in osx, go here:
https://gist.github.com/nrollr/4daba07c67adcb30693e
openssl genrsa -out key.pem
openssl req -new -key key.pem -out csr.pem
openssl x509 -req -days 9999 -in csr.pem -signkey key.pem -out cert.pem
**/
app.use(cookieParser(sessionSecret));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.set('appName', 'z2b-chapter07');
app.set('port', appEnv.port);
app.set('views', path.join(__dirname + '/HTML'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/HTML'));
app.use(bodyParser.json());
// Define your own router file in controller folder, export the router, add it into the index.js.
// app.use('/', require("./controller/yourOwnRouter"));
app.use('/', require("./controller/restapi/router"));
if (cfenv.getAppEnv().isLocal == true){
var server = app.listen(app.get('port'), function() {
console.log('Listening locally on port %d', server.address().port);
});
}
else{
var server = app.listen(app.get('port'), function() {
console.log('Listening remotely on port %d', server.address().port);
});
}
/**
* load any file requested on the server
* @param {express.req} req - the inbound request object from the client
* @param {express.res} res - the outbound response object for communicating back to client
* @function
*/
function loadSelectedFile(req, res) {
var uri = req.originalUrl;
var filename = __dirname + "/HTML" + uri;
fs.readFile(filename,
function(err, data) {
if (err) {
console.log('Error loading ' + filename + ' error: ' + err);
return res.status(500).send('Error loading ' + filename);
}
var type = mime.lookup(filename);
res.setHeader('content-type', type);
res.writeHead(200);
res.end(data);
});
}