1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-23 01:36:28 +02:00
onion/server.js
Brett Sun 7eaa3b1a2b Inject environment variables into app
Injects environment variables into the app through webpack, rather than
script snippets in index.html.

As part of this:

* Updated server.js to use node’s path package to resolve file paths
* Ensure that all url environment variables passed to the app don’t
have a trailing slash, allowing for cleaner template urls

**Note**: There are still a number of constants that should be taken
out of the app and put into environment variables.
2016-06-02 17:14:58 +02:00

30 lines
1.1 KiB
JavaScript

const express = require('express');
const compression = require('compression');
const path = require('path');
const removeTrailingSlash = require('remove-trailing-slash');
const BASE_PATH = removeTrailingSlash(process.env.ONION_BASE_PATH || '/');
const PORT = process.env.ONION_PORT || 4000;
const app = express();
app.use(compression());
app.use(path.join(BASE_PATH, '/static/js'), express.static(path.resolve(__dirname, 'dist/js')));
app.use(path.join(BASE_PATH, '/static/css'), express.static(path.resolve(__dirname, 'dist/css')));
app.use(path.join(BASE_PATH, '/static/fonts'), express.static(path.resolve(__dirname, 'dist/fonts')));
app.use(path.join(BASE_PATH, '/static/third_party'), express.static(path.resolve(__dirname, 'dist/third_party')));
app.get(/.*/, function(req, res) {
console.log('%s %s', req.method, req.path);
res.sendFile(path.resolve(__dirname, 'dist/index.html'));
});
if (require.main === module) {
console.log(`Starting Onion server on port ${PORT} with basePath set to ${BASE_PATH || '/'}`);
app.listen(PORT);
}
module.exports.app = app;