-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathindex.js
43 lines (33 loc) · 1.22 KB
/
index.js
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
'use strict';
var path = require('path');
var fs = require('fs');
var cleanBaseURL = require('../../../../utilities/clean-base-url');
function HistorySupportAddon(project) {
this.project = project;
this.name = 'history-support-middleware';
}
HistorySupportAddon.prototype.serverMiddleware = function(config) {
var app = config.app;
var options = config.options;
var watcher = options.watcher;
var baseURL = cleanBaseURL(options.baseURL);
var baseURLRegexp = new RegExp('^' + baseURL);
var locationType = this.project.config(options.environment).locationType;
if (['auto', 'history'].indexOf(locationType) !== -1) {
app.use(function(req, res, next) {
watcher.then(function(results) {
var acceptHeaders = req.headers.accept || [];
var hasHTMLHeader = acceptHeaders.indexOf('text/html') !== -1;
var isForBaseURL = baseURLRegexp.test(req.path);
if (hasHTMLHeader && isForBaseURL && req.method === 'GET') {
var assetPath = req.path.slice(baseURL.length);
if (!fs.existsSync(path.join(results.directory, assetPath))) {
req.url = baseURL + 'index.html';
}
}
next();
});
});
}
};
module.exports = HistorySupportAddon;