forked from microsoft/WSL2-Linux-Kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
109 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
import express from 'express'; | ||
const app = express(); | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
app.get('/', (req, res) => { | ||
const name = process.env.NAME || 'World'; | ||
res.send(`Hello ${name}!`); | ||
}); | ||
'use strict'; | ||
|
||
// [START functions_helloworld_get] | ||
const functions = require('@google-cloud/functions-framework'); | ||
|
||
const port = parseInt(process.env.PORT) || 8080; | ||
app.listen(port, () => { | ||
console.log(`helloworld: listening on port ${port}`); | ||
// Register an HTTP function with the Functions Framework that will be executed | ||
// when you make an HTTP request to the deployed function's endpoint. | ||
functions.http('helloGET', (req, res) => { | ||
res.send('Hello World!'); | ||
}); | ||
// [END functions_helloworld_get] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
{ | ||
"name": "helloworld", | ||
"description": "Simple hello world sample in Node", | ||
"version": "1.0.0", | ||
"name": "nodejs-docs-samples-functions-hello-world-get", | ||
"version": "0.0.1", | ||
"private": true, | ||
"main": "index.js", | ||
"type": "module", | ||
"scripts": { | ||
"start": "node index.js" | ||
"license": "Apache-2.0", | ||
"author": "Google Inc.", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" | ||
}, | ||
"engines": { | ||
"node": ">=16.0.0" | ||
}, | ||
"author": "Google LLC", | ||
"license": "Apache-2.0", | ||
"scripts": { | ||
"test": "c8 mocha -p -j 2 test/*.test.js --timeout=6000 --exit" | ||
}, | ||
"dependencies": { | ||
"express": "^4.17.1" | ||
"@google-cloud/functions-framework": "^3.1.0" | ||
}, | ||
"devDependencies": { | ||
"c8": "^10.0.0", | ||
"gaxios": "^6.0.0", | ||
"mocha": "^10.0.0", | ||
"wait-port": "^1.0.4" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2017 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
const assert = require('assert'); | ||
const {request} = require('gaxios'); | ||
const {exec} = require('child_process'); | ||
const waitPort = require('wait-port'); | ||
|
||
const startFF = async (target, signature, port) => { | ||
const ff = exec( | ||
`npx functions-framework --target=${target} --signature-type=${signature} --port=${port}` | ||
); | ||
await waitPort({host: 'localhost', port}); | ||
return ff; | ||
}; | ||
|
||
const httpInvocation = (fnUrl, port) => { | ||
const baseUrl = `http://localhost:${port}`; | ||
|
||
// GET request | ||
return request({ | ||
url: `${baseUrl}/${fnUrl}`, | ||
}); | ||
}; | ||
|
||
describe('index.test.js', () => { | ||
describe('functions_helloworld_get helloGET', () => { | ||
const PORT = 8081; | ||
let ffProc; | ||
|
||
before(async () => { | ||
ffProc = await startFF('helloGET', 'http', PORT); | ||
}); | ||
|
||
after(() => ffProc.kill()); | ||
|
||
it('helloGET: should print hello world', async () => { | ||
const response = await httpInvocation('helloGET', PORT); | ||
assert.strictEqual(response.status, 200); | ||
assert.strictEqual(response.data, 'Hello World!'); | ||
}); | ||
}); | ||
}); |