Stackedit/server/index.js

70 lines
2.3 KiB
JavaScript
Raw Normal View History

2017-09-26 22:54:26 +00:00
var compression = require('compression');
var serveStatic = require('serve-static');
var path = require('path');
2017-09-29 18:43:26 +00:00
module.exports = function (app, serveV4) {
2017-09-26 22:54:26 +00:00
// Use gzip compression
2017-09-27 20:27:12 +00:00
if (process.env.NODE_ENV === 'production') {
app.all('*', function(req, res, next) {
2017-09-29 18:43:26 +00:00
// Force HTTPS on stackedit.io
2017-09-27 20:27:12 +00:00
if (req.headers.host === 'stackedit.io' && !req.secure && req.headers['x-forwarded-proto'] !== 'https') {
return res.redirect('https://stackedit.io' + req.url);
}
2017-09-29 18:43:26 +00:00
// Enable CORS for fonts
if (/\.(eot|ttf|woff|svg)$/.test(req.url)) {
res.header('Access-Control-Allow-Origin', '*');
}
2017-09-27 20:27:12 +00:00
next();
});
app.use(compression());
}
2017-09-26 22:54:26 +00:00
app.get('/oauth2/githubToken', require('./github').githubToken);
2017-09-29 18:43:26 +00:00
if (serveV4) {
app.post('/pdfExport', require('../stackedit_v4/app/pdf').export);
app.post('/sshPublish', require('../stackedit_v4/app/ssh').publish);
app.post('/picasaImportImg', require('../stackedit_v4/app/picasa').importImg);
app.get('/downloadImport', require('../stackedit_v4/app/download').importPublic);
}
2017-09-26 22:54:26 +00:00
// Serve callback.html in /app
app.get('/oauth2/callback', function(req, res) {
2017-09-27 20:27:12 +00:00
res.sendFile(path.join(__dirname, '../static/oauth2/callback.html'));
2017-09-26 22:54:26 +00:00
});
// Serve static resources
2017-09-27 20:27:12 +00:00
if (process.env.NODE_ENV === 'production') {
2017-09-29 18:43:26 +00:00
if (serveV4) {
// Serve landing.html in /
app.get('/', function(req, res) {
res.sendFile(require.resolve('../stackedit_v4/views/landing.html'));
});
// Serve editor.html in /viewer
app.get('/editor', function(req, res) {
res.sendFile(require.resolve('../stackedit_v4/views/editor.html'));
});
// Serve viewer.html in /viewer
app.get('/viewer', function(req, res) {
res.sendFile(require.resolve('../stackedit_v4/views/viewer.html'));
});
}
2017-09-27 20:27:12 +00:00
// Serve index.html in /app
app.get('/app', function(req, res) {
res.sendFile(path.join(__dirname, '../dist/index.html'));
});
2017-09-26 22:54:26 +00:00
2017-09-29 18:43:26 +00:00
app.use(serveStatic(path.join(__dirname, '../dist')));
2017-09-27 20:27:12 +00:00
2017-09-29 18:43:26 +00:00
if (serveV4) {
app.use(serveStatic(path.dirname(require.resolve('../stackedit_v4/public/cache.manifest'))));
// Error 404
app.use(function(req, res) {
res.status(404).sendFile(require.resolve('../stackedit_v4/views/error_404.html'));
});
}
2017-09-27 20:27:12 +00:00
}
2017-09-26 22:54:26 +00:00
};