-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathserver.js
74 lines (61 loc) · 2.43 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
'use strict';
var express = require('express'),
path = require('path'),
fs = require('fs'),
mongoose = require('mongoose'),
formsAngular = require('forms-angular'),
fngAudit = require('fng-audit'),
fngJqUpload = require('fng-jq-upload');
/**
* Main application file
*/
// Set default node environment to development
process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var config = require('./lib/config/config');
mongoose.connect(config.mongo.uri, config.mongo.options);
mongoose.Promise = global.Promise;
mongoose.set('debug',true);
// Setup Express
var app = express();
require('./lib/config/express')(app);
var fngHandler = new (formsAngular)(mongoose, app, {
urlPrefix: '/api/',
plugins: {
JQMongoFileUploader: {plugin: fngJqUpload.Controller, options: { } },
fngAudit: {plugin: fngAudit.controller, options:{ }}
}
});
// Bootstrap forms-angular controlled models
var modelsPath = path.join(__dirname, 'app/models');
fs.readdirSync(modelsPath).forEach(function (file) {
var fname = modelsPath + '/' + file;
if ((app.get('env') === 'test' || file.slice(0,4) !== 'test') && fs.statSync(fname).isFile()) {
var fngModelInfo = require(fname);
fngHandler.newResource(fngModelInfo.model, fngModelInfo.options);
}
});
// If we are starting the server to run e2e tests then add a little data
if (app.get('env') === 'test') {
var exec = require('child_process').exec;
var dataPath = path.join(__dirname, 'test/e2e/e2edata');
var dataFiles = fs.readdirSync(dataPath);
var mongoHost = config.mongo.uri.match(/mongodb:\/\/(.*\d)/)[1];
dataFiles.forEach(function (file) {
var fname = dataPath + '/' + file;
if (fs.statSync(fname).isFile()) {
console.log('mongoimport --host ' + mongoHost + ' --db fng-test --drop --collection ' + file.slice(0, -3) + 's --jsonArray < ' + fname);
exec('mongoimport --host ' + mongoHost + ' --db fng-test --drop --collection ' + file.slice(0, -3) + 's --jsonArray < ' + fname,
function (error, stdout, stderr) {
if (error !== null) {
console.log('Error importing models : ' + error + ' (Code = ' + error.code + ' ' + error.signal + ') : ' + stderr + ' : ' + stdout);
}
});
}
});
}
// Start server
app.listen(config.port, config.ip, function () {
console.log('Express server listening on %s:%d, in %s mode', config.ip, config.port, app.get('env'));
});
// Expose app
exports = module.exports = app;