Skip to content

Commit be23ca8

Browse files
committed
Add option to serve a custom index html file
1 parent 375b59b commit be23ca8

2 files changed

Lines changed: 22 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ corresponding environment variable will be ignored.
5757
| `--path-regexp` | `PATH_REGEXP` | A regular expression to verify a given upload path. This should be set with care, because it may allow write access to outside the upload directory. | `/^[a-zA-Z0-9-_/]*$/` |
5858
| `--disable-auto-port` | `DISABLE_AUTO_PORT` | Disable automatic port increase if the port is nor available. | Not set. |
5959
| `--enable-folder-creation` | `ENABLE_FOLDER_CREATION` | Enable automatic folder creation when uploading file to non-existent folder. | Not set. |
60+
| `--index-file` | `INDEX_FILE` | Use a custom html file as index instead of the default internal index. If used, the form fields need to have the same names as in the original index. | Not set. |
6061
| `--help`, `-h` | | Show some help text | |
6162

6263
Examples:

http-server-upload.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ let token = process.env.TOKEN || false;
2525
let pathMatchRegExp = (process.env.PATH_REGEXP) ? new RegExp(process.env.PATH_REGEXP) : /^[a-zA-Z0-9-_/]*$/;
2626
let maxFileSize = (parseInt(process.env.MAX_FILE_SIZE, 10) || 200) * 1024 * 1024;
2727
let enableFolderCreation = !!process.env.ENABLE_FOLDER_CREATION;
28+
let indexFile = process.env.INDEX_FILE || false;
2829

2930
console.log('HTTP Server Upload');
3031

@@ -65,6 +66,10 @@ Argument | Environmen variable
6566
Disable automatic port increase if the port is nor available. [Not set]
6667
--enable-folder-creation | ENABLE_FOLDER_CREATION
6768
Enable automatic folder creation when uploading file to non-existent folder. [Not set]
69+
--index-file | INDEX_FILE
70+
Use a custom html file as index instead of the default internal index.
71+
If used, the form fields need to have the same names as in
72+
the original index. [Not set]
6873
--help or -h
6974
Show this help text.
7075
@@ -135,6 +140,9 @@ while (myArgs.length > 0) {
135140
case '--max-file-size':
136141
maxFileSize = (parseInt(val, 10) || 200) * 1024 * 1024;
137142
break;
143+
case '--index-file':
144+
indexFile = val;
145+
break;
138146

139147
default:
140148
console.warn(`WANRING: Unknown command line argument: ${key}`);
@@ -277,6 +285,19 @@ server.on('request', async (req, res) => {
277285
} else {
278286
// show index page
279287
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
288+
289+
if (indexFile) {
290+
// try to load custom index file
291+
try {
292+
const index = await fs.readFile(indexFile, { encoding: 'utf-8' });
293+
res.write(index);
294+
return res.end();
295+
} catch (err) {
296+
console.log(`Error serving custom index file! ${err}`);
297+
}
298+
}
299+
300+
// default index file
280301
res.write(`<!DOCTYPE html>
281302
<html lang="en">
282303
<head>

0 commit comments

Comments
 (0)