@gormed/tus-node-server

Node.js tus server

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@gormed/tus-node-server
525360.3.94 years ago4 years agoMinified + gzip package size for @gormed/tus-node-server in KB

Readme

tus-node-server
npm version Build Status Coverage Status Dependency Status
tus is a new open protocol for resumable uploads built on HTTP. This is the tus protocol 1.0.0 node.js server implementation.

Installation

$ npm install tus-node-server

Flexible Data Stores

  • Local File Storage
```js
server.datastore = new tus.FileStore({
    path: '/files'
});
```
  • Google Cloud Storage
```js

server.datastore = new tus.GCSDataStore({
    path: '/files',
    projectId: 'project-id',
    keyFilename: 'path/to/your/keyfile.json',
    bucket: 'bucket-name',
});
```
  • Amazon S3
```js

server.datastore = new tus.S3Store({
    path: '/files',
    bucket: 'bucket-name',
    accessKeyId: 'access-key-id',
    secretAccessKey: 'secret-access-key',
    region: 'eu-west-1',
    partSize: 8 * 1024 * 1024, // each uploaded part will have ~8MB,
    tmpDirPrefix: 'tus-s3-store',
});
```

Quick Start

Use the tus-node-deploy Docker image

$ docker run -p 1080:8080 -d bhstahl/tus-node-deploy

Build a standalone server yourself

const tus = require('tus-node-server');

const server = new tus.Server();
server.datastore = new tus.FileStore({
    path: '/files'
});

const host = '127.0.0.1';
const port = 1080;
server.listen({ host, port }, () => {
    console.log(`[${new Date().toLocaleTimeString()}] tus server listening at http://${host}:${port}`);
});

Use tus-node-server as Express Middleware

const tus = require('tus-node-server');
const server = new tus.Server();
server.datastore = new tus.FileStore({
    path: '/files'
});

const app = express();
const uploadApp = express();
uploadApp.all('*', server.handle.bind(server));
app.use('/uploads', uploadApp);

const host = '127.0.0.1';
const port = 1080;
app.listen(port, host);

Use tus-node-server with Koa or plain Node server

const http = require('http');
const url = require('url');
const Koa = require('koa')
const tus = require('tus-node-server');
const tusServer = new tus.Server();

const app = new Koa();
const appCallback = app.callback();
const port = 1080;

tusServer.datastore = new tus.FileStore({
    path: '/files',
});

const server = http.createServer((req, res) => {
    const urlPath = url.parse(req.url).pathname;

    // handle any requests with the `/files/*` pattern
    if (/^\/files\/.+/.test(urlPath.toLowerCase())) {
        return tusServer.handle(req, res);
    }

    appCallback(req, res);
});

server.listen(port)

Features

Events:

Execute code when lifecycle events happen by adding event handlers to your server.
const Server = require('tus-node-server').Server;
const EVENTS = require('tus-node-server').EVENTS;

const server = new Server();
server.on(EVENTS.EVENT_UPLOAD_COMPLETE, (event) => {
    console.log(`Upload complete for file ${event.file.id}`);
});

  • EVENT_FILE_CREATED: Fired when a POST request successfully creates a new file
_Example payload:_
```
{
    file: {
        id: '7b26bf4d22cf7198d3b3706bf0379794',
        upload_length: '41767441',
        upload_metadata: 'filename NDFfbWIubXA0'
     }
}
```
  • EVENT_ENDPOINT_CREATED: Fired when a POST request successfully creates a new upload endpoint
_Example payload:_
```
{
    url: 'http://localhost:1080/files/7b26bf4d22cf7198d3b3706bf0379794'
}
```
  • EVENT_UPLOAD_COMPLETE: Fired when a PATCH request finishes writing the file
_Example payload:_
```
{
    file: {
        id: '7b26bf4d22cf7198d3b3706bf0379794',
        upload_length: '41767441',
        upload_metadata: 'filename NDFfbWIubXA0'
    }
}
```

Custom GET handlers:

Add custom GET handlers to suit your needs, similar to Express routing.
const server = new Server();
server.get('/uploads', (req, res) => {
    // Read from your DataStore
    fs.readdir(server.datastore.path, (err, files) => {
        // Format the JSON response and send it
    }
});

Custom file names:

const fileNameFromUrl = (req) => {
    return req.url.replace(/\//g, '-');
}

server.datastore = new tus.FileStore({
    path: '/files',
    namingFunction: fileNameFromUrl
});

Development

Start the demo server using Local File Storage
$ npm run demo

Or start up the demo server using Google Cloud Storage
$ npm run gcs_demo

Then navigate to the demo (localhost:1080) which uses tus-js-client