[go: nahoru, domu]

Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
feat: Add support for other response content-types
Browse files Browse the repository at this point in the history
The new option `mimeType` lets you respond with any file type you want.

Closes #7
  • Loading branch information
mischah committed Dec 3, 2017
1 parent a379c8c commit c9a7d12
Show file tree
Hide file tree
Showing 16 changed files with 3,439 additions and 1,737 deletions.
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ npm run start:dev
```

This way the server uses `nodemon` to restart itself on changes.
This way you dont have to restart the server in case you changed an endpoint.
This way you don’t have to restart the server in case you changed an endpoint.


### Later (eg. for tests in CI)
Expand All @@ -69,7 +69,7 @@ This way you dont have to restart the server in case you changed an endpoint.
npm start
```

Just fires up the server via node.
Just starts the server via node.
This one comes is without any magic (eg. `foreverjs`)

## Configuring endpoints
Expand All @@ -87,12 +87,41 @@ module.exports = SetupEndpoint({
name: 'simpleExample',
urls: [{
requests: [
{ response: '/json-templates/simpleExample.json' }
{ response: '/response-files/simpleExample.json' }
]
}]
});
```

#### Serving different content types

`/server/api/fileTypes.js`:

```js
module.exports = SetupEndpoint({
name: 'fileTypes',
urls: [{
params: '/json',
requests: [{
response: '/response-files/simpleExample.json'
}]
}, {
params: '/text',
requests: [{
response: '/response-files/example.txt',
mimeType: 'text/plain'
}]
}, {
params: '/html',
requests: [{
response: '/response-files/example.html',
statusCode: 201,
mimeType: 'text/html'
}]
}]
});
```

#### Advanced Example

`/server/api/anotherExample.js`:
Expand All @@ -104,7 +133,7 @@ module.exports = SetupEndpoint({
params: '/read',
requests: [{
method: 'GET',
response: '/json-templates/anotherExample.json'
response: '/response-files/anotherExample.json'
}]
}, {
params: '/update/{id}',
Expand Down Expand Up @@ -156,7 +185,7 @@ module.exports = SetupEndpoint({
requests: [{
// Returns a 401 error provided by boom
// as defined on endpoint level
response: '/json-templates/anotherExample.json'
response: '/response-files/anotherExample.json'
}]
}
],
Expand Down Expand Up @@ -185,9 +214,12 @@ The configuration object in Detail:
* is used to define the http method(s) to which the endpoint will listen.
* `urls.requests.response`
* Could be a string pointing to a JSON template:
* `response: '/json-templates/articles.json'`
* `response: '/response-files/articles.json'`
* Or just a JavaScript object:
* `response: { success: true }`
* `urls.requests.mimeType`
* optional (string). Defaults to `application/json`.
* is used to set the `content-type` response header.
* `urls.requests.statusCode`
* Optional
* A status code (number)
Expand Down
Loading

0 comments on commit c9a7d12

Please sign in to comment.