UNPKG

3.97 kBJavaScriptView Raw
1'use strict';
2
3/**
4 * This is the Form.io application server.
5 */
6const express = require('express');
7const nunjucks = require('nunjucks');
8const fs = require('fs-extra');
9const path = require('path');
10const util = require('./src/util/util');
11require('colors');
12const Q = require('q');
13const cors = require('cors');
14const test = process.env.TEST_SUITE;
15const noInstall = process.env.NO_INSTALL;
16
17module.exports = function(options) {
18 options = options || {};
19 const q = Q.defer();
20
21 util.log('');
22 const rl = require('readline').createInterface({
23 input: require('fs').createReadStream('logo.txt')
24 });
25
26 rl.on('line', function(line) {
27 util.log(
28 line.substring(0,4) +
29 line.substring(4, 30).cyan.bold +
30 line.substring(30, 33) +
31 line.substring(33, 42).green.bold +
32 line.substring(42)
33 );
34 });
35
36 rl.on('close', function() {
37 // Print the welcome screen.
38 util.log('');
39 util.log(fs.readFileSync('welcome.txt').toString().green);
40 });
41
42 // Use the express application.
43 const app = options.app || express();
44
45 // Use the given config.
46 const config = options.config || require('config');
47
48 // Configure nunjucks.
49 nunjucks.configure('client', {
50 autoescape: true,
51 express: app
52 });
53
54 //cors configuration
55 if (config.allowedOrigins) {
56 app.use(cors({
57 origin: function(origin, callback) {
58 if (!origin) {
59 return callback(null, true);
60 }
61 if (config.allowedOrigins.indexOf(origin) === -1 && config.allowedOrigins.indexOf("*") === -1) {
62 var msg = 'The CORS policy for this site does not allow access from the specified Origin.';
63 return callback(new Error(msg), false);
64 }
65 return callback(null, true);
66 }
67 }));
68 }
69 // Mount the client application.
70 app.use('/', express.static(path.join(__dirname, '/client/dist')));
71
72 // Load the form.io server.
73 const server = options.server || require('./index')(config);
74 const hooks = options.hooks || {};
75
76 app.use(server.formio.middleware.restrictRequestTypes);
77 server.init(hooks).then(function(formio) {
78 // Called when we are ready to start the server.
79 const start = function() {
80 // Start the application.
81 if (fs.existsSync('app')) {
82 const application = express();
83 application.use('/', express.static(path.join(__dirname, '/app/dist')));
84 config.appPort = config.appPort || 8080;
85 application.listen(config.appPort);
86 const appHost = `http://localhost:${config.appPort}`;
87 util.log(` > Serving application at ${appHost.green}`);
88 }
89
90 // Mount the Form.io API platform.
91 app.use(options.mount || '/', server);
92
93 // Allow tests access server internals.
94 app.formio = formio;
95
96 // Listen on the configured port.
97 return q.resolve({
98 server: app,
99 config: config
100 });
101 };
102
103 // Which items should be installed.
104 const install = {
105 download: false,
106 extract: false,
107 import: false,
108 user: false
109 };
110
111 // Check for the client folder.
112 if (!fs.existsSync('client') && !test && !noInstall) {
113 install.download = true;
114 install.extract = true;
115 }
116
117 // See if they have any forms available.
118 formio.db.collection('forms').estimatedDocumentCount(function(err, numForms) {
119 // If there are forms, then go ahead and start the server.
120 if ((!err && numForms > 0) || test || noInstall) {
121 if (!install.download && !install.extract) {
122 return start();
123 }
124 }
125
126 // Import the project and create the user.
127 install.import = true;
128 install.user = true;
129
130 // Install.
131 require('./install')(formio, install, function(err) {
132 if (err) {
133 if (err !== 'Installation canceled.') {
134 return util.log(err.message);
135 }
136 }
137
138 // Start the server.
139 start();
140 });
141 });
142 });
143
144 return q.promise;
145};