-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
101 lines (87 loc) · 2.04 KB
/
app.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
var createError = require("http-errors");
const http = require("http");
var express = require("express");
const co = require("co");
const PORT = process.env.PORT || 3100;
var path = require("path");
var cookieParser = require("cookie-parser");
//var logger = require("chpr-logger");
const { configure } = require("./config/express");
var mongoose = require("mongoose");
const { port } = require("./config");
require("mongoose-paginate-v2");
const url =
"mongodb+srv://root:[email protected]/PiDev?retryWrites=true&w=majority";
let app;
let server;
mongoose.connect(url, { useNewUrlParser: true, useUnifiedTopology: true });
const mongo = mongoose.connection;
mongo.on("connected", () => {
console.log("initialisation");
});
mongo.on("open", () => {
console.log("connexion etablie");
console.log(PORT);
});
mongo.on("error", (err) => {
console.log(err);
});
/**
* Start the web app.
*
* @returns {Promise} when app end to start
*/
async function start() {
if (app) {
return app;
}
//logger.info("Express web server creation");
app = configure(express());
server = http.createServer(app);
require("./api/product/index")(app);
require("./api/comment/index")(app);
require("./api/reaction/index")(app);
if (process.env.NODE_ENV == "production") {
app.use(express.static('client/build'));
const path = require('path');
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname,'client','build','index.html'))
})
}
await server.listen(PORT);
//logger.info(
//{
// port: server.address().port,
// environment: process.env.NODE_ENV,
// },
// "✔ Server running"
//);
return app;
}
/**
* Stop the web app.
*
* @returns {Promise} when app end to start
*/
async function stop() {
if (server) {
await server.close();
server = null;
app = null;
}
await mongoose.disconnect();
return Promise.resolve();
}
if (!module.parent) {
co(start);
}
module.exports = {
start,
stop,
get server() {
return server;
},
get app() {
return app;
},
};