Skip to content

Commit 4bc5dbc

Browse files
committed
feat(Server): Add option to configure routes path
Defaults to /files
1 parent b1da212 commit 4bc5dbc

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

src/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ const opt = nodegetopt.create([
77
['', 'totalsize=TOTALSIZE', "total size parameter (default 'totalsize')"],
88
['', 'storageType=TYPE', "disk or memory (default 'memory')"],
99
['', 'storagePath=PATH', "where to save files (default '/tmp')"],
10+
['', 'route=flies', "the API starting path (default '/files')"],
1011
]).bindHelp().parseSystem().options;
1112

1213
server.run({
13-
chunkNumber: opt.chunknumber,
1414
port: (opt.port || process.env.PORT || 5000),
15+
chunkNumber: opt.chunknumber,
1516
totalSize: opt.totalsize,
1617
storage: {
1718
type: opt.storageType,
1819
path: opt.storagePath,
1920
},
21+
route: (opt.route || 'files'),
2022
});

src/server.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import express from 'express';
33
import cors from 'cors';
44
import multer, { diskStorage, memoryStorage } from 'multer';
55
import { urlencoded, json } from 'body-parser';
6+
67
import { writeFile, getFileSize, readFile, removeFile, writeFileChunk, assembleFileChunks } from './file-service';
78
import logger from './log';
89

@@ -28,8 +29,6 @@ export default {
2829
init(options) {
2930
let storage;
3031
if (options.storage.type === 'disk') {
31-
logger.info('Using disk storage', (options.storage.path || '/tmp'));
32-
3332
if (options.storage.path && !existsSync(options.storage.pathh)) {
3433
logger.error(options.storage.path, 'does not exist');
3534
throw new Error(`${options.storage.path} does not exist`);
@@ -56,29 +55,29 @@ export default {
5655
app.use(json());
5756
app.use(cors());
5857

59-
app.get('/files/:filename/size', (request, response) => {
58+
app.get(`/${options.route}/:filename/size`, (request, response) => {
6059
const result = getFileSize(request.params.filename);
6160
response.status(result.status).send(result.data);
6261
});
6362

64-
app.get('/files/:filename', (request, response) => {
63+
app.get(`/${options.route}/:filename`, (request, response) => {
6564
const result = readFile(request.params.filename);
6665
response.status(result.status).send(result.data);
6766
});
68-
app.delete('/files/:filename', (request, response) => {
67+
app.delete(`/${options.route}/:filename`, (request, response) => {
6968
const result = removeFile(request.params.filename);
7069
response.status(result.status);
7170
});
7271

73-
app.post('/files', upload.single('file'), (request, response) => {
72+
app.post(`/${options.route}`, upload.single('file'), (request, response) => {
7473
saveFile(request, response, request.file.originalname);
7574
});
7675

77-
app.post('/files/:filename', upload.single('file'), (request, response) => {
76+
app.post(`/${options.route}/:filename`, upload.single('file'), (request, response) => {
7877
saveFile(request, response, request.params.filename);
7978
});
8079

81-
app.post('/chunk/:filename', upload.single('file'), (request, response) => {
80+
app.post(`/${options.route}/chunk/:filename`, upload.single('file'), (request, response) => {
8281
const result = writeFileChunk(
8382
request.params.filename,
8483
request.file.buffer,
@@ -87,7 +86,7 @@ export default {
8786
response.status(result.status).send(result.data);
8887
});
8988

90-
app.post('/assemble/:filename', (request, response) => {
89+
app.post(`/${options.route}/assemble/:filename`, (request, response) => {
9190
const result = assembleFileChunks(
9291
request.params.filename,
9392
request.body[options.totalSize || 'totalsize']

0 commit comments

Comments
 (0)