You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When developing with Serverless deploying functions to AWS after each change might be annoying. This plugin allows you to simulate API Gateway locally, so all function calls can be done on localhost.
4
+
5
+
### Differences with Serverless-serve-plugin
6
+
7
+
See 'Credits and inspiration'.
8
+
9
+
### Requierements
10
+
11
+
Node v4 and over.
12
+
13
+
### Installation
14
+
15
+
In your Serverless project:
16
+
17
+
```
18
+
npm install serverless-offline
19
+
```
20
+
21
+
Then in `s-project.json` add following entry to the plugins array: `serverless-offline`
22
+
23
+
Like this:
24
+
```
25
+
"plugins": ["serverless-offline"]
26
+
```
27
+
28
+
And in main project root do:
29
+
30
+
```
31
+
sls offline start
32
+
```
33
+
34
+
### Command line options
35
+
36
+
`--prefix``-p`: Add prefix to the URLs, so your clients will not use `http://localhost:3000/` but `http://localhost:3000/prefix/` instead. Default: empty
37
+
38
+
`--port``-P`: Port to listen on. Default: `3000`
39
+
40
+
41
+
### Usage
42
+
43
+
Just send your requests to `http://localhost:3000/` as it would be API Gateway.
44
+
45
+
Using this plugin with [Nodemon](https://github.com/remy/nodemon) is advised to reload your local code after every change.
46
+
47
+
### Usage with Babel
48
+
49
+
Optionaly, your handlers can be required with `babel-register`.
50
+
To do so, in your `s-project.json` file, set options to be passed to babel-register like this:
51
+
```
52
+
{
53
+
/* ... */
54
+
"custom": {
55
+
"serverless-offline": {
56
+
"babelOptions": {
57
+
/* Your own options, example: */
58
+
presets: ["es2015", "stage-2"]
59
+
}
60
+
}
61
+
},
62
+
"plugins": ["serverless-offline", /* ... */]
63
+
}
64
+
```
65
+
To view the full list of babel-register options, click [here](https://babeljs.io/docs/usage/require/)
66
+
67
+
### Simulation quality
68
+
69
+
This plugin simulates API Gateway for many practical purposes, good enough for development - but is not a perfect simulator. Specifically, Lambda currently runs on Node v0.10.13, whereas *offline* runs on your own runtime where no timeout or memory limits are enforced. Mapping templates are not simulated, so are security checks. You will probably find other differences.
70
+
71
+
### Credits and inspiration
72
+
73
+
This plugin is a fork of [Nopik](https://github.com/Nopik/)'s [Serverless-serve](https://github.com/Nopik/serverless-serve), the differences are:
74
+
75
+
- Under the hood, *Serve* uses Express, *Offline* uses Hapi.
76
+
-*Serve*'s `event` object (passed to your handlers) is undocumented ad often empty. *Offline*'s `event` object is defined by: `Object.assign({ isServerlessOffline: true }, request);` where `request` is [Hapi's request object](http://hapijs.com/api#request-object). This allows you to quickly access properties like the request's params or payload in your lambda handler:
77
+
```javascript
78
+
module.exports.handler=function(event, context) {
79
+
+var params;
80
+
+
81
+
+if (event.isServerlessOffline) { // Offline
82
+
+ params =event.params;
83
+
+ } else { // Online
84
+
+/* Define your event object using a template in your s-function.json file */
85
+
+ params =event.customKeyDefinedInTemplate;
86
+
+ }
87
+
+};
88
+
```
89
+
-*Serve* will pick the first non `default` response of an endpoint if `errorPattern` is undefined. Doing so, it neglects the `default` answer (so it does not work out of the box with `serverless project create`). This causes new projects to answer 400 using *Serve*.
90
+
Example :
91
+
```javascript
92
+
"responses": {
93
+
"400": {
94
+
"statusCode":"400"// errorPattern is undefined: Serve will always answer 400
95
+
},
96
+
"default": {
97
+
"statusCode":"200",
98
+
"responseParameters": {},
99
+
"responseModels": {},
100
+
"responseTemplates": {
101
+
"application/json":""
102
+
}
103
+
}
104
+
}
105
+
```
106
+
-*Offline* dropped support for *Serve*'s optional init script for now.
0 commit comments