22
33> 👉 ** Note** : since 1.0.0 packages are split and published under the ` @tus ` scope.
44> The old package, ` tus-node-server ` , is considered unstable and will only receive security fixes.
5- > Make sure to use the new package, currently in beta at ` 1.0.0-beta.5 ` .
5+ > Make sure to use the new package, currently in beta at ` 1.0.0-beta.7 ` .
66
77## Contents
88
@@ -35,16 +35,16 @@ npm install @tus/server
3535A standalone server which stores files on disk.
3636
3737``` js
38- const {Server } = require (' @tus/server' )
39- const {FileStore } = require (' @tus/file-store' )
40- const host = ' 127.0.0.1'
41- const port = 1080
38+ const { Server } = require (" @tus/server" );
39+ const { FileStore } = require (" @tus/file-store" );
40+ const host = " 127.0.0.1" ;
41+ const port = 1080 ;
4242
4343const server = new Server ({
44- path: ' /files' ,
45- datastore: new FileStore ({directory: ' ./files' }),
46- })
47- server .listen ({host, port})
44+ path: " /files" ,
45+ datastore: new FileStore ({ directory: " ./files" }),
46+ });
47+ server .listen ({ host, port });
4848```
4949
5050## API
@@ -177,93 +177,94 @@ server.on(EVENTS.POST_TERMINATE, (req, res, id => {})
177177### Example: integrate tus into Express
178178
179179` ` ` js
180- const {Server } = require (' @tus/server' )
181- const {FileStore } = require (' @tus/file-store' )
182- const express = require (' express' )
183-
184- const host = ' 127.0.0.1'
185- const port = 1080
186- const app = express ()
187- const uploadApp = express ()
180+ const { Server } = require (" @tus/server" );
181+ const { FileStore } = require (" @tus/file-store" );
182+ const express = require (" express" );
183+
184+ const host = " 127.0.0.1" ;
185+ const port = 1080 ;
186+ const app = express ();
187+ const uploadApp = express ();
188188const server = new Server ({
189- datastore: new FileStore ({directory: ' /files' }),
190- })
189+ datastore: new FileStore ({ directory: " /files" }),
190+ });
191191
192- uploadApp .all (' * ' , server .handle .bind (server))
193- app .use (' /uploads' , uploadApp)
194- app .listen (port, host)
192+ uploadApp .all (" * " , server .handle .bind (server));
193+ app .use (" /uploads" , uploadApp);
194+ app .listen (port, host);
195195` ` `
196196
197197### Example: integrate tus into Koa
198198
199199` ` ` js
200- const http = require (' node:http' )
201- const url = require (' node:url' )
202- const Koa = require (' koa' )
203- const {Server } = require (' @tus/server' )
204- const {FileStore } = require (' @tus/file-store' )
205-
206- const app = new Koa ()
207- const appCallback = app .callback ()
208- const port = 1080
200+ const http = require (" node:http" );
201+ const url = require (" node:url" );
202+ const Koa = require (" koa" );
203+ const { Server } = require (" @tus/server" );
204+ const { FileStore } = require (" @tus/file-store" );
205+
206+ const app = new Koa ();
207+ const appCallback = app .callback ();
208+ const port = 1080 ;
209209const tusServer = new Server ({
210- path: ' /files' ,
211- datastore: new FileStore ({directory: ' /files' }),
212- })
210+ path: " /files" ,
211+ datastore: new FileStore ({ directory: " /files" }),
212+ });
213213
214214const server = http .createServer ((req , res ) => {
215- const urlPath = url .parse (req .url ).pathname
215+ const urlPath = url .parse (req .url ).pathname ;
216216
217217 // handle any requests with the `/files/*` pattern
218218 if (/ ^ \/ files\/ . + / .test (urlPath .toLowerCase ())) {
219- return tusServer .handle (req, res)
219+ return tusServer .handle (req, res);
220220 }
221221
222- appCallback (req, res)
223- })
222+ appCallback (req, res);
223+ });
224224
225- server .listen (port)
225+ server .listen (port);
226226` ` `
227227
228228### Example: integrate tus into Fastify
229229
230230` ` ` js
231- const fastify = require (' fastify' )({ logger: true })
232- const {Server } = require (' @tus/server' )
233- const {FileStore } = require (' @tus/file-store' )
231+ const fastify = require (" fastify" )({ logger: true });
232+ const { Server } = require (" @tus/server" );
233+ const { FileStore } = require (" @tus/file-store" );
234234
235235const tusServer = new Server ({
236- path: ' /files' ,
237- datastore: new FileStore ({ directory: ' ./files' }),
238- })
236+ path: " /files" ,
237+ datastore: new FileStore ({ directory: " ./files" }),
238+ });
239239
240240/**
241241 * add new content-type to fastify forewards request
242242 * without any parser to leave body untouched
243243 * @see https://www.fastify.io/docs/latest/Reference/ContentTypeParser/
244244 */
245245fastify .addContentTypeParser (
246- ' application/offset+octet-stream' , (request , payload , done ) => done (null )
247- )
246+ " application/offset+octet-stream" ,
247+ (request , payload , done ) => done (null )
248+ );
248249
249250/**
250251 * let tus handle preparation and filehandling requests
251252 * fastify exposes raw nodejs http req/res via .raw property
252253 * @see https://www.fastify.io/docs/latest/Reference/Request/
253254 * @see https://www.fastify.io/docs/latest/Reference/Reply/#raw
254255 */
255- fastify .all (' /files' , (req , res ) => {
256- tusServer .handle (req .raw , res .raw )
257- })
258- fastify .all (' /files/*' , (req , res ) => {
259- tusServer .handle (req .raw , res .raw )
260- })
256+ fastify .all (" /files" , (req , res ) => {
257+ tusServer .handle (req .raw , res .raw );
258+ });
259+ fastify .all (" /files/*" , (req , res ) => {
260+ tusServer .handle (req .raw , res .raw );
261+ });
261262fastify .listen (3000 , (err ) => {
262263 if (err) {
263- fastify .log .error (err)
264- process .exit (1 )
264+ fastify .log .error (err);
265+ process .exit (1 );
265266 }
266- })
267+ });
267268` ` `
268269
269270### Example: integrate tus into Next.js
@@ -273,9 +274,9 @@ Attach the tus server handler to a Next.js route handler in an [optional catch-a
273274` / pages/ api/ upload/ [[... file]].ts `
274275
275276` ` ` ts
276- import type {NextApiRequest , NextApiResponse } from ' next'
277- import {Server , Upload } from ' @tus/server'
278- import {FileStore } from ' @tus/file-store'
277+ import type { NextApiRequest , NextApiResponse } from " next" ;
278+ import { Server , Upload } from " @tus/server" ;
279+ import { FileStore } from " @tus/file-store" ;
279280
280281/**
281282 * !Important. This will tell Next.js NOT Parse the body as tus requires
@@ -285,40 +286,40 @@ export const config = {
285286 api: {
286287 bodyParser: false ,
287288 },
288- }
289+ };
289290
290291const tusServer = new Server ({
291292 // `path` needs to match the route declared by the next file router
292293 // ie /api/upload
293- path: ' /api/upload' ,
294- datastore: new FileStore ({directory: ' ./files' }),
295- })
294+ path: " /api/upload" ,
295+ datastore: new FileStore ({ directory: " ./files" }),
296+ });
296297
297298export default function handler (req : NextApiRequest , res : NextApiResponse ) {
298- return tusServer .handle (req, res)
299+ return tusServer .handle (req, res);
299300}
300301` ` `
301302
302303### Example: validate metadata when an upload is created
303304
304305` ` ` js
305- const {Server } = require (' @tus/server' )
306+ const { Server } = require (" @tus/server" );
306307// ...
307308
308309const server = new Server ({
309310 // ..
310311 async onUploadCreate (req , res , upload ) {
311- const {ok , expected , received } = validateMetadata (upload)
312+ const { ok , expected , received } = validateMetadata (upload);
312313 if (! ok) {
313- const body = ` Expected "${ expected} " in "Upload-Metadata" but received "${ received} "`
314- throw {status_code: 500 , body} // if undefined, falls back to 500 with "Internal server error".
314+ const body = ` Expected "${ expected} " in "Upload-Metadata" but received "${ received} "` ;
315+ throw { status_code: 500 , body }; // if undefined, falls back to 500 with "Internal server error".
315316 }
316317 // We have to return the (modified) response.
317- return res
318+ return res;
318319 },
319- })
320+ });
320321
321- server .listen ({host, port})
322+ server .listen ({ host, port });
322323` ` `
323324
324325## Types
0 commit comments