summaryrefslogtreecommitdiff
path: root/alarm/node_modules/node-forge/nodejs/server.js
blob: 175bd560f6114806045d9ab09db409abff8d1fc4 (plain)
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
var PATH = require('path');
var express = require('express');
var PORT = 8083;

exports.main = function(callback) {
  try {
    var app = express();

    mountStaticDir(app, /^\/forge\/(.*)$/, PATH.join(__dirname, '../js'));
    mountStaticDir(app, /^\/test\/(.*)$/, PATH.join(__dirname, 'test'));
    mountStaticDir(app, /^\/mocha\/(.*)$/, PATH.join(__dirname, 'node_modules/mocha'));
    mountStaticDir(app, /^\/chai\/(.*)$/, PATH.join(__dirname, 'node_modules/chai'));
    app.get(/^\//, express.static(PATH.join(__dirname, 'ui')));

    var server = app.listen(PORT);

    console.log('open http://localhost:' + PORT + '/');

    return callback(null, {
      server: server,
      port: PORT
    });
  } catch(err) {
    return callback(err);
  }
};

function mountStaticDir(app, route, path) {
  app.get(route, function(req, res, next) {
    var originalUrl = req.url;
    req.url = req.params[0];
    express.static(path)(req, res, function() {
      req.url = originalUrl;
      return next.apply(null, arguments);
    });
  });
}

if(require.main === module) {
  exports.main(function(err) {
    if(err) {
      console.error(err.stack);
      process.exit(1);
    }
  });
}