From d56650c5498c1469f6dca4bb6b8f29834a479a48 Mon Sep 17 00:00:00 2001 From: Candid Dauth Date: Tue, 18 Oct 2016 14:27:20 +0300 Subject: [PATCH] Serve frontend files from server using express --- bower.json | 3 +++ frontend/gulpfile.js | 9 ++++++--- frontend/index.html | 2 +- package.json | 7 ++++++- server/server.js | 21 +++++++++++++++++---- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/bower.json b/bower.json index 4298beac..208b7b5e 100644 --- a/bower.json +++ b/bower.json @@ -34,6 +34,9 @@ "osmtogeojson": "^2.2.12", "leaflet-fullHash": "https://raw.githubusercontent.com/KoGor/leaflet-fullHash/master/leaflet-fullHash.js" }, + "resolutions": { + "leaflet": "^1.0.1" + }, "overrides": { "bootstrap": { "main": [ diff --git a/frontend/gulpfile.js b/frontend/gulpfile.js index f04139c0..32e94b68 100644 --- a/frontend/gulpfile.js +++ b/frontend/gulpfile.js @@ -17,6 +17,7 @@ var combine = require("stream-combiner"); var sourcemaps = require("gulp-sourcemaps"); var inject = require("gulp-inject"); var img64 = require("./gulpfile-img64"); +var path = require("path"); var files = [ "app/**/*.js", @@ -36,7 +37,7 @@ gulp.task("clean", function() { gulp.task("deps", function() { return combine( - gulp.src(mainBowerFiles({ paths: { bowerDirectory: '../bower_components', bowerJson: '../bower.json' } }), { base: process.cwd() + "/" }), + gulp.src(mainBowerFiles({ paths: { bowerDirectory: __dirname + '/../bower_components', bowerJson: __dirname + '/../bower.json' } }), { base: path.resolve(__dirname + "/..") + "/" }), gulpIf([ "**/*.js", "**/*.css" ], combine( gulpIf("**/*.js", combine( newer("build/dependencies.js"), @@ -105,7 +106,8 @@ gulp.task("all", [ "deps", "app" ], function() { sourcemaps.init({ loadMaps: true }), concat("all.css"), sourcemaps.write("./sourcemaps") - ) + ), + gulp.src("deref.html") ), gulp.dest("build") ); @@ -115,8 +117,9 @@ gulp.task("index", [ "all" ], function() { return combine( gulp.src("index.html"), img64(), + concat("build/index.html"), inject(gulp.src([ "build/all.js", "build/all.css" ], { read: false }), { relative: true, removeTags: true }), - gulp.dest("build") + gulp.dest(".") ); }); diff --git a/frontend/index.html b/frontend/index.html index 3ee542ed..8635a31f 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -36,6 +36,6 @@ - + \ No newline at end of file diff --git a/package.json b/package.json index 220171d1..4d021280 100644 --- a/package.json +++ b/package.json @@ -4,11 +4,16 @@ "author": "Candid Dauth ", "version": "0.0.1", "scripts": { - "start": "node server/server.js" + "start": "npm run deps && npm run build && npm run server", + "deps": "npm update --dev && bower update", + "build": "gulp", + "server": "node server/server.js", + "clean": "gulp clean" }, "dependencies": { "cheerio": "^0.22.0", "compressjs": "^1.0.3", + "express": "^4.14.0", "promise": "^7.1.1", "request": "^2.75.0", "request-promise": "^4.1.1", diff --git a/server/server.js b/server/server.js index 6f439158..82b633bb 100644 --- a/server/server.js +++ b/server/server.js @@ -9,6 +9,10 @@ var routing = require("./routing"); var gpx = require("./gpx"); var search = require("./search"); var Promise = require("promise"); +var express = require("express"); +var path = require("path"); + +var frontendPath = path.resolve(__dirname + "/../frontend"); Object.defineProperty(Error.prototype, "toJSON", { value: function() { @@ -25,9 +29,18 @@ Object.defineProperty(Error.prototype, "toJSON", { var dbP = database.connect(); -var app = http.createServer(); -var appP = Promise.denodeify(app.listen.bind(app))(config.port, config.host).then(function() { - var io = socketIo.listen(app); +var app = express(); + +app.use(express.static(frontendPath + "/build/")); + +app.get("/:padId", function(req, res) { + res.sendFile(frontendPath + "/build/index.html"); +}); + +var server = http.createServer(app); + +var serverP = Promise.denodeify(server.listen.bind(server))(config.port, config.host).then(function() { + var io = socketIo.listen(server); io.sockets.on("connection", function(socket) { var d = domain.create(); @@ -309,7 +322,7 @@ var appP = Promise.denodeify(app.listen.bind(app))(config.port, config.host).the }); }); -Promise.all([ dbP, appP ]).then(function() { +Promise.all([ dbP, serverP ]).then(function() { console.log("Server started on " + (config.host || "*" ) + ":" + config.port); }).catch(function(err) { console.error(err);