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
Our real-world examples use local testing. Check our [serverless-examples repository](https://github.com/scaleway/serverless-examples) for more information.
26
-
</Message>
27
-
28
-
# Local testing
29
18
30
19
Scaleway Serverless Functions can run outside Scaleway infrastructures. This is useful to set up, develop, and test functions locally. You can run functions directly on your machines to debug them and analyze logs.
31
20
32
21
Each local testing framework is written in its respective language and emulates the calls made to functions by the Scaleway infrastructure.
33
22
The result is a language-specific executable or script that users can connect to with their favorite debugging and logging tools.
34
23
24
+
## Local testing quickstarts
25
+
26
+
<Tabsid="frameworks">
27
+
28
+
<TabsTablabel="NodeJS">
29
+
30
+
Refer to the [NodeJS local testing repository](https://github.com/scaleway/serverless-functions-node) for more information on testing your function locally using Node.
31
+
32
+
**Quickstart**
33
+
34
+
1. Install the Scaleway Serverless Functions package using `npm`:
35
+
```sh
36
+
npm i @scaleway/serverless-functions
37
+
```
38
+
39
+
2. Add the following code to the file containing your handle:
40
+
41
+
For ES modules
42
+
```js
43
+
// handler.js
44
+
45
+
import { pathToFileURL } from"url";
46
+
47
+
functionhandle(event, context, callback) {
48
+
return {
49
+
statusCode:201,
50
+
body:JSON.stringify({
51
+
message:"Hello World!",
52
+
}),
53
+
headers: {
54
+
"Content-Type":"application/json",
55
+
},
56
+
};
57
+
}
58
+
59
+
// This part will execute when testing locally, but not when the function is deployed online
60
+
if (import.meta.url === pathToFileURL(process.argv[1]).href) {
3. In a terminal, run the command below to execute your file and start the local webserver:
93
+
94
+
```sh
95
+
node handler.js
96
+
```
97
+
98
+
4. In another terminal session, run the command below:
99
+
```sh
100
+
curl -XGET http://localhost:8080
101
+
```
102
+
103
+
The function returns the content of its body:
104
+
105
+
```js
106
+
{"message":"Hello World!"}%
107
+
```
108
+
</TabsTab>
109
+
110
+
<TabsTab label="Python">
111
+
112
+
Refer to the [Python local testing repository](https://github.com/scaleway/serverless-functions-python) for more information on testing your functions locally using Python.
113
+
114
+
**Quickstart**
115
+
116
+
1. Install the Scaleway Serverless Functions package using `pip`:
117
+
```sh
118
+
pip install scaleway-functions-python
119
+
```
120
+
121
+
2. Add the following code to the file containing your handle:
122
+
123
+
```python
124
+
# handler.py
125
+
126
+
# Standard entrypoint to a Scaleway serverless function
# The import is conditional so that you do not need
134
+
# to package the library when deploying on Scaleway Functions.
135
+
from scaleway_functions_python import local
136
+
local.serve_handler(handler, port=8080)
137
+
```
138
+
139
+
3. In a terminal, run the command below to execute your file and start the local webserver:
140
+
141
+
```sh
142
+
python handler.py
143
+
```
144
+
145
+
4. In another terminal session, run the command below:
146
+
```sh
147
+
curl http://localhost:8080
148
+
```
149
+
150
+
The function returns the expected output:
151
+
152
+
```python
153
+
Hello World!
154
+
```
155
+
156
+
<Message type="note">
157
+
The function above only processes GET requests, as declared in its code. Other requests will return the defined error message:
158
+
159
+
```sh
160
+
curl -XPOST http://localhost:8080
161
+
> Invalid method!
162
+
```
163
+
164
+
</Message>
165
+
166
+
</TabsTab>
167
+
168
+
<TabsTab label="Go">
169
+
170
+
Refer to the [Go local testing repository](https://github.com/scaleway/serverless-functions-go) for more information on testing your functions locally using Go.
171
+
172
+
**Quickstart**
173
+
174
+
1. Install the Scaleway Serverless Functions package using `go get`:
175
+
```sh
176
+
go get github.com/scaleway/serverless-functions-go
177
+
```
178
+
179
+
2. In a new folder, create a file named `handler.go` and add the following code to it:
180
+
```go
181
+
package handler
182
+
183
+
import (
184
+
"encoding/json"
185
+
"net/http"
186
+
)
187
+
188
+
// This handle function comes from our examples and is not modified.
3. Create a `main.go` file in a new `cmd` subfolder, then add the following code to it:
215
+
216
+
```go
217
+
package main
218
+
219
+
import (
220
+
// "localfunc" is the module name located in your go.mod. To generate a go.mod with "localfunc" as a name, you can use the following command : go mod init localfunc.
221
+
222
+
// Otherwise, you can replace "localfunc" with the name of your own module.
0 commit comments