facilmap/server/server.js

77 wiersze
2.0 KiB
JavaScript
Czysty Zwykły widok Historia

2014-04-07 00:33:49 +00:00
var http = require("http");
2016-10-30 14:01:21 +00:00
var compression = require("compression");
2014-04-09 23:58:14 +00:00
var domain = require("domain");
var Promise = require("promise");
var express = require("express");
var path = require("path");
var webpack = require("webpack");
var webpackConfig = require("../frontend/webpack.config");
var webpackMiddleware = require("webpack-dev-middleware");
2016-10-30 14:01:21 +00:00
var config = require("../config");
var Database = require("./database/database");
var utils = require("./utils");
var Socket = require("./socket");
var frontendPath = path.resolve(__dirname + "/../frontend");
2014-04-07 00:33:49 +00:00
2014-12-14 03:43:05 +00:00
Object.defineProperty(Error.prototype, "toJSON", {
value: function() {
2014-12-27 02:47:40 +00:00
var str = this.message;
if(this.errors) {
for(var i=0; i<this.errors.length; i++)
str += "\n"+this.errors[i].message;
}
return str;
2014-12-14 03:43:05 +00:00
},
configurable: true
});
2016-10-30 14:01:21 +00:00
process.on('unhandledRejection', (reason, promise) => {
console.trace("Unhandled rejection", reason);
});
2016-10-30 14:01:21 +00:00
utils.promiseAuto({
database: () => new Database(),
2016-10-30 14:01:21 +00:00
databaseConnect: database => database.connect(),
2014-04-18 18:11:50 +00:00
2016-10-30 14:01:21 +00:00
server: () => {
var app = express();
app.use(compression());
app.get("/bundle-:hash.js", function(req, res, next) {
res.setHeader('Cache-Control', 'public, max-age=31557600'); // one year
next();
});
var staticMiddleware = process.env.FM_DEV
? webpackMiddleware(webpack(webpackConfig), {
publicPath: "/"
})
: express.static(frontendPath + "/build/");
app.use(staticMiddleware);
app.get("/:padId", function(req, res, next) {
req.url = req.url.replace(/[^\/]*$/, "");
req.originalUrl = req.originalUrl.replace(/[^\/]*$/, "");
staticMiddleware(req, res, next);
});
2016-10-30 14:01:21 +00:00
var server = http.createServer(app);
return Promise.denodeify(server.listen.bind(server))(config.port, config.host).then(() => server);
},
2014-04-07 00:33:49 +00:00
2016-10-30 14:01:21 +00:00
socket: (server, database) => {
return new Socket(server, database);
}
}).then(res => {
console.log("Server started on " + (config.host || "*" ) + ":" + config.port);
2016-10-30 14:01:21 +00:00
}).catch(err => {
console.error(err);
process.exit(1);
2016-10-30 14:01:21 +00:00
});