Skip to content

Commit a6c432c

Browse files
committed
fix: ensure spa works with default indexFile of index.html
1 parent c98e72f commit a6c432c

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

bin/cli.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ const dir = args.directory || '.';
2323

2424
const log = function(request, response, statusCode) {
2525
const d = new Date();
26+
/* c8 ignore next 3 -- Time-dependent */
2627
const seconds = d.getSeconds() < 10 ? '0' + d.getSeconds() : d.getSeconds(),
2728
minutes = d.getMinutes() < 10 ? '0' + d .getMinutes() : d.getMinutes(),
2829
hours = d.getHours() < 10 ? '0' + d .getHours() : d.getHours(),
2930
datestr = hours + ':' + minutes + ':' + seconds,
3031

3132
line = datestr + ' [' + response.statusCode + ']: ' + request.url;
3233
let colorized = line;
33-
if (tty.isatty(process.stdout.fd))
34+
/* c8 ignore next 5 -- Environment */
35+
if (tty.isatty(process.stdout.fd)) {
3436
colorized = (response.statusCode >= 500) ? colors.red.bold(line) :
3537
(response.statusCode >= 400) ? colors.red(line) :
3638
line;
39+
}
3740
console.log(colorized);
3841
};
3942

@@ -77,7 +80,7 @@ const server = http.createServer(function (request, response) {
7780
// npm start -- --spa --index-file test/fixtures/there/index.html
7881
// with http://127.0.0.1:8080/test/fixtures/there?email=john.cena
7982
if (args['spa'] && !new URL(request.url, 'http://localhost').pathname.includes(".")) {
80-
file.serveFile(args['index-file'], 200, {}, request, response);
83+
file.serveFile(args['index-file'] || 'index.html', 200, {}, request, response);
8184
} else {
8285
file.serve(request, response, callback);
8386
}
@@ -89,6 +92,7 @@ const hostAddress = args['host-address'] || '127.0.0.1';
8992

9093
if (hostAddress === '127.0.0.1') {
9194
server.listen(port);
95+
/* c8 ignore next 3 -- Not working with localhost or 0.0.0.0 */
9296
} else {
9397
server.listen(port, hostAddress);
9498
}

test/integration/binary.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,30 @@ describe('node-static (CLI)', function () {
136136
assert.equal(text, 'hello world', 'should respond with hello world');
137137
});
138138

139+
it('serving file within directory and spa and default indexFile', async function () {
140+
const {response /* , stdout */} = await spawnConditional(binFile, [
141+
fixturePath, '--spa'
142+
], timeout - 9000, {
143+
condition: 'serving as a single page app',
144+
error (err) {
145+
console.log('err', err);
146+
},
147+
action: (/* err, stdout */) => {
148+
return fetch(
149+
`http://localhost:8080/some/other/path`
150+
);
151+
}
152+
});
153+
154+
const {status} = response;
155+
const contentType = response.headers.get('content-type');
156+
// const text = await response.text();
157+
158+
assert.equal(status, 200, 'should respond with 200');
159+
assert.equal(contentType, 'text/html', 'should respond with text/html');
160+
// assert.contains(text, 'hello world', 'should respond with hello world');
161+
});
162+
139163
it('serving file within directory with headers', async function () {
140164
const {response /* , stdout */} = await spawnConditional(binFile, [
141165
'-p', this.port,

test/integration/node-static-test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ describe('node-static', function () {
240240
method: 'HEAD'
241241
});
242242
assert.equal(response.status, 200, 'should respond with 200');
243-
assert.isEmpty(await response.text(), 'head must has no body');
243+
assert.isEmpty(await response.text(), 'head must have no body');
244244
});
245245

246246
it('requesting headers', async function () {

0 commit comments

Comments
 (0)