Skip to content

Commit 5c62c9f

Browse files
committed
Document fastboot.method and fastboot.body
1 parent 3fc604e commit 5c62c9f

2 files changed

Lines changed: 90 additions & 2 deletions

File tree

app/app.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import config from './config/environment';
55

66
let App;
77

8-
Ember.MODEL_FACTORY_INJECTIONS = true;
9-
108
App = Ember.Application.extend({
119
modulePrefix: config.modulePrefix,
1210
podModulePrefix: config.podModulePrefix,

markdown/docs/user-guide.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,96 @@ export default Ember.Route.extend({
257257
});
258258
```
259259

260+
#### Request Method
261+
262+
You can also access the method of the request via `fastboot.request` in the `fastboot` service.
263+
The `method` property will return the method name (`GET`, `POST`, `PATCH`...) of the request.
264+
265+
```javascript
266+
export default Ember.Route.extend({
267+
fastboot: Ember.inject.service(),
268+
269+
model() {
270+
let method = this.get('fastboot.request.method');
271+
// ...
272+
}
273+
});
274+
```
275+
276+
#### Request Body
277+
278+
On `POST`, `PUT` and `PATCH` request you will probably be interested in the body of the request.
279+
You can access the `body` of the request via `fastboot.request` in the `fastboot` service, but
280+
only of you setup a middleware to extract the body of the request in your express server.
281+
282+
You can use `body-parser` and `fastboot-express-middleware` and to create an in-repo addon that
283+
contains a middleware.
284+
285+
286+
```javascript
287+
// your-app/lib/fastboot-middleware/index.js
288+
var bodyParser = require('body-parser');
289+
var FastBootExpressMiddleware = require('fastboot-express-middleware');
290+
291+
module.exports = {
292+
name: 'fastboot-middleware',
293+
294+
serverMiddleware(options) {
295+
var app = options.app;
296+
app.use(bodyParser.text()); // or bodyParser.json()
297+
app.use(function(req, resp, next) {
298+
var outputPath = process.env['EMBER_DIST_FOLDER'];
299+
300+
var fastbootMiddleware = FastBootExpressMiddleware({
301+
distPath: outputPath
302+
});
303+
304+
fastbootMiddleware(req, resp, next);
305+
});
306+
}
307+
};
308+
```
309+
310+
```javascript
311+
// your-app/lib/fastboot-middleware/package.json
312+
{
313+
"name": "post-middlware",
314+
"keywords": [
315+
"ember-addon"
316+
]
317+
}
318+
```
319+
320+
Then on the `package.json` of your app you can the folder of this in-repo addon to the autodiscovery
321+
paths:
322+
323+
```javascript
324+
// your-app/package.json
325+
{
326+
// ...
327+
"ember-addon": {
328+
"paths": [
329+
"lib/fastboot-middleware"
330+
]
331+
}
332+
}
333+
```
334+
335+
Now that this middleware is parsing the body of request, you can access it on the `fastboot` service.
336+
Depending on what method on `body-parser` you used, the body can be a parse JSON object, a string
337+
or even a raw Buffer.
338+
339+
```javascript
340+
export default Ember.Route.extend({
341+
fastboot: Ember.inject.service(),
342+
343+
model() {
344+
let method = this.get('fastboot.request.body');
345+
// ...
346+
}
347+
});
348+
```
349+
260350
### FastBoot Response
261351

262352
FastBoot Response gives you access to the response metadata that FastBoot will send back the client.

0 commit comments

Comments
 (0)