-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
126 lines (102 loc) · 2.98 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
120
121
122
123
124
125
126
const express = require("express"),
path = require("path"),
bodyParser = require("body-parser"),
cors = require("cors"),
mongoose = require("mongoose"),
config = require("./DB"),
passport = require("passport"),
errorHandler = require("errorhandler");
var http = require("http");
var server = http.createServer(function(req, res) {
res.end("test");
});
server.on("listening", function() {
console.log("ok, server is running");
});
const userRoute = require('./routes/user.routes');
const gadgetRoute = require("./routes/gadget.routes");
const panelRoute = require("./routes/panel.routes");
const accountRoute = require("./routes/account.routes");
//Configure smart contracts
const TecModule = require('./app/TEC_server');
const TecRoute = require('./routes/TEC.routes');
const MatchingModule = require('./app/Matching_server');
const MatchingRoute = require('./routes/Matching.routes');
//
//Configure mongoose's promise to global promise
mongoose.Promise = global.Promise;
//Configure isProduction variable
const isProduction = process.env.NODE_ENV === "production";
//Initiate our app
const app = express();
//Configure our app
app.use(cors());
app.use(require("morgan")("dev"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, "../dist/energy")));
if (!isProduction) {
app.use(errorHandler());
}
//Configure Mongoose
mongoose.connect(config.DB, { useNewUrlParser: true }).then(
() => {
console.log("Database is connected");
},
err => {
console.log("Cannot connect to the database" + err);
}
);
//Middleware
require("./models/User");
require("./config/passport");
app.use(require("./routes"));
//Use Routes
app.use("/panels", panelRoute);
app.use("/gadgets", gadgetRoute);
app.use("/accounts", accountRoute);
app.use("/users", userRoute);
// use smart contract routes
app.use('/tec',TecRoute);
app.use('/match',MatchingRoute);
//
// error handlers
if (!isProduction) {
app.use((err, req, res) => {
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: err
}
});
});
}
app.use((err, req, res) => {
res.status(err.status || 500);
res.json({
errors: {
message: err.message,
error: {}
}
});
});
// Catch unauthorised errors
app.use(function(err, req, res, next) {
if (err.name === "UnauthorizedError") {
res.status(401);
res.json({ message: err.name + ": " + err.message });
}
});
app.get("/getversion", function(req, res) {
console.log("Version " + version);
res.status(200).json({ version: version });
});
var version = process.env.version || "1.0";
const port = process.env.PORT || 4000;
server = app.listen(port, function() {
console.log("Listening on port " + port);
console.log("Version " + version);
TecModule.deployTec();
MatchingModule.deployMatching();
});