forked from formio/formio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
107 lines (92 loc) · 2.58 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
'use strict';
/**
* This is the Form.io application server.
*/
var express = require('express');
var config = require('config');
var nunjucks = require('nunjucks');
var fs = require('fs-extra');
var util = require('./src/util/util');
require('colors');
util.log('');
var rl = require('readline').createInterface({
input: require('fs').createReadStream('logo.txt')
});
rl.on('line', function(line) {
util.log(
line.substring(0,4) +
line.substring(4, 30).cyan.bold +
line.substring(30, 33) +
line.substring(33, 42).green.bold +
line.substring(42)
);
});
rl.on('close', function() {
// Print the welcome screen.
util.log('');
util.log(fs.readFileSync('welcome.txt').toString().green);
});
// Get the express application.
var server = express();
// Configure nunjucks.
nunjucks.configure('client', {
autoescape: true,
express: server
});
// Mount the client application.
server.use('/', express.static(__dirname + '/client/dist'));
// Load the form.io server.
var formioServer = require('./index')(config);
formioServer.init().then(function(formio) {
// Called when we are ready to start the server.
var start = function() {
// Start the application.
if (fs.existsSync('app')) {
var app = express();
app.use('/', express.static(__dirname + '/app/dist'));
config.appPort = config.appPort || 8080;
app.listen(config.appPort);
var appHost = 'http://localhost:' + config.appPort;
util.log(' > Serving application at ' + appHost.green);
}
// Mount the Form.io API platform at /api.
server.use('/', formioServer);
// Listen on the configured port.
util.log(' > Serving the Form.io API Platform at ' + config.domain.green);
server.listen(config.port);
};
// Which items should be installed.
var install = {
download: false,
extract: false,
import: false,
user: false
};
// Check for the client folder.
if (!fs.existsSync('client')) {
install.download = true;
install.extract = true;
}
// See if they have any forms available.
formio.db.collection('forms').count(function(err, numForms) {
// If there are forms, then go ahead and start the server.
if (!err && numForms > 0) {
if (!install.download && !install.extract) {
return start();
}
}
else {
// Import the project and create the user.
install.import = true;
install.user = true;
}
// Install.
require('./install')(formio, install, function(err) {
if (err) {
return util.log(err.message);
}
// Start the server.
start();
});
});
});