-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.ts
53 lines (42 loc) · 1.4 KB
/
server.ts
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
import * as express from "express";
import * as http from "http";
import * as reload from "reload";
import * as mongoose from "mongoose";
import * as path from "path";
// Constants
const port = 8080;
// App
const app = express();
// provide paths to static files
app.use("/js", express.static(__dirname + '/public/js'));
app.use("/css", express.static(__dirname + '/public/css'));
app.use("/views", express.static(__dirname + '/views'));
// view engine setup
app.set('view engine', 'pug');
app.set('views', path.join(__dirname, 'views'));
let Greeting;
app.get('/', (req, res) => {
Greeting.findOne((err, greeting) => {
res.render('index', {title: "Dockerized MEAN Greeter", greeting: greeting.text});
})
});
const server = http.createServer(app);
reload(server, app);
server.listen(port, () => {
const host = server.address().address;
console.log("Webapp listening at http://%s:%s", host, port)
});
if (mongoose.connection.readyState === 0) {
/* 0 = disconnected, 1 = connected, 2 = connecting, 3 = disconnecting */
mongoose.connect('mongodb://mongodb:27017/db');
mongoose.connection.on('connected', () => {
log("Connection to MongoDB established.");
Greeting = mongoose.model('Greeting',
new mongoose.Schema({text: String}),
'greetings'); // collection name
});
}
function log(message:any) {
const date = new Date();
console.log(date.toISOString() + " - ",message);
}