Skip to content

Commit dde3a7a

Browse files
authored
Merge pull request #82 from cibernox/document-method-and-body
Document `fastboot.method` and `fastboot.body`
2 parents 3fc604e + 63ac0e0 commit dde3a7a

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

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: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,100 @@ 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+
if (req.serveUrl) {
301+
var fastbootMiddleware = FastBootExpressMiddleware({
302+
distPath: outputPath
303+
});
304+
305+
fastbootMiddleware(req, resp, next);
306+
} else {
307+
next();
308+
}
309+
});
310+
}
311+
};
312+
```
313+
314+
```javascript
315+
// your-app/lib/fastboot-middleware/package.json
316+
{
317+
"name": "post-middlware",
318+
"keywords": [
319+
"ember-addon"
320+
]
321+
}
322+
```
323+
324+
Then on the `package.json` of your app you can the folder of this in-repo addon to the autodiscovery
325+
paths:
326+
327+
```javascript
328+
// your-app/package.json
329+
{
330+
// ...
331+
"ember-addon": {
332+
"paths": [
333+
"lib/fastboot-middleware"
334+
]
335+
}
336+
}
337+
```
338+
339+
Now that this middleware is parsing the body of request, you can access it on the `fastboot` service.
340+
Depending on what method on `body-parser` you used, the body can be a parse JSON object, a string
341+
or even a raw Buffer.
342+
343+
```javascript
344+
export default Ember.Route.extend({
345+
fastboot: Ember.inject.service(),
346+
347+
model() {
348+
let method = this.get('fastboot.request.body');
349+
// ...
350+
}
351+
});
352+
```
353+
260354
### FastBoot Response
261355

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

0 commit comments

Comments
 (0)