Mock Config logoMock config
Mocking responses

File responses

Respond to a route with a file.

Mock Config can answer a route with a file straight from disk. Pass rest.file() with a path, and the route sends that file back as its response.

Respond With A File

import { rest } from 'mock-config-server';

rest.get('/report', rest.file('./files/report.pdf'));

Every GET /report request receives the contents of report.pdf as the response.

Match A File Response

Pass matcher conditions in the route settings argument when a file response should only apply to some requests.

import { rest } from 'mock-config-server';

rest.get('/report', rest.file('./files/report.pdf'), {
  match: { headers: { accept: 'application/pdf' } }
});

Force A Download

By default a file is sent inline where the client can render it. To send it as a download instead, set it as an attachment from a resolver with attachment.

rest.get('/report', ({ attachment }) => {
  attachment('report.pdf');
  // ...
});

attachment marks the response as a downloadable file with the given filename, so the browser saves it rather than displaying it.

If you just want to expose a folder of files over HTTP rather than wire each one to a route, you may not need file at all — serve them as static files instead.

On this page