This repository was archived by the owner on May 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
63 lines (54 loc) · 1.78 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
var express = require('express')
, app = express()
, bodyParser = require('body-parser')
, validator = require('express-validator')
, morgan = require('morgan')
, mongoose = require('mongoose')
, config = require('./config/config')
, fs = require('fs')
, os = require('os')
, interfaces = os.networkInterfaces()
, addrs = []
, db = {}
;
// Set config.host ip
for (var k in interfaces) {
for (var k2 in interfaces[k]) {
var address = interfaces[k][k2]
if(address.family == 'IPv4' && !address.internal)
addrs.push(address.address)
}
}
config.host = addrs.pop();
// Connect to mongo db
mongoose.connect(config.db);
// use body parser as middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(validator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
field : formParam,
message : msg
};
}
}));
app.set('secret', config.secret);
// config mongoose models
var modelsPath = __dirname + '/models';
fs.readdirSync(modelsPath).forEach(function (file) {
if (file.indexOf('.js') >= 0)
db[file.replace('.js', '')] = require(modelsPath + '/' + file)(mongoose)
});
require('./config/routes')(mongoose, express, app, db);
// Use morgan to log requests to the console
app.use(morgan('dev'));
app.use(config.uploadDir, express.static(config.uploadPath));
app.listen(config.port);
console.log("Server is working on http://" + config.host + ":" + config.port);