diff --git a/appengine-standard/getting-started/build/app.yaml b/appengine-standard/getting-started/build/app.yaml new file mode 100644 index 0000000000..e938832ccb --- /dev/null +++ b/appengine-standard/getting-started/build/app.yaml @@ -0,0 +1,16 @@ +# Copyright 2018, Google, Inc. +# 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. + +# [START app_yaml] +runtime: nodejs8 +# [END app_yaml] diff --git a/appengine-standard/getting-started/build/package.json b/appengine-standard/getting-started/build/package.json new file mode 100644 index 0000000000..415dcd3014 --- /dev/null +++ b/appengine-standard/getting-started/build/package.json @@ -0,0 +1,29 @@ +{ + "name": "appengine-standard-getting-started-build-apps", + "version": "0.0.1", + "description": "Node.js getting started sample for building an app on App Engine standard environment.", + "private": true, + "main": "server.js", + "scripts": { + "lint": "samples lint", + "pretest": "npm run lint", + "test": "ava --verbose test/*.test.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": "8.x.x" + }, + "author": "Google Inc.", + "license": "Apache-2.0", + "dependencies": { + "express": "^4.16.3" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "1.4.17", + "ava": "0.22.0", + "semistandard": "^12.0.1" + } +} diff --git a/appengine-standard/getting-started/build/server.js b/appengine-standard/getting-started/build/server.js new file mode 100644 index 0000000000..a9918495c2 --- /dev/null +++ b/appengine-standard/getting-started/build/server.js @@ -0,0 +1,33 @@ +/** + * Copyright 2018, Google, Inc. + * 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. + */ + +'use strict'; + +// [START app] +const express = require('express'); +const app = express(); + +app.get('/', (req, res) => { + res.send('Hello from App Engine!'); +}); + +// Listen to the App Engine-specified port, or 8080 otherwise +const PORT = process.env.PORT || 8080; +app.listen(PORT, () => { + console.log(`Server listening on port ${PORT}...`); +}); +// [END app] + +module.exports = app; diff --git a/appengine-standard/getting-started/build/test/server.test.js b/appengine-standard/getting-started/build/test/server.test.js new file mode 100644 index 0000000000..45323f68fe --- /dev/null +++ b/appengine-standard/getting-started/build/test/server.test.js @@ -0,0 +1,36 @@ +// Copyright 2018, Google, Inc. +// 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. + +'use strict'; + +const path = require(`path`); +const test = require(`ava`); +const utils = require(`@google-cloud/nodejs-repo-tools`); + +const cwd = path.join(__dirname, `../`); +const requestObj = utils.getRequest({ + cwd: cwd, + cmd: `server` }); + +test.beforeEach(utils.stubConsole); +test.afterEach.always(utils.restoreConsole); + +test.cb.serial(`should send greetings`, (t) => { + requestObj + .get(`/`) + .expect(200) + .expect((response) => { + t.is(response.text, `Hello from App Engine!`); + }) + .end(t.end); +}); diff --git a/appengine-standard/getting-started/update/app.yaml b/appengine-standard/getting-started/update/app.yaml new file mode 100644 index 0000000000..e938832ccb --- /dev/null +++ b/appengine-standard/getting-started/update/app.yaml @@ -0,0 +1,16 @@ +# Copyright 2018, Google, Inc. +# 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. + +# [START app_yaml] +runtime: nodejs8 +# [END app_yaml] diff --git a/appengine-standard/getting-started/update/package.json b/appengine-standard/getting-started/update/package.json new file mode 100644 index 0000000000..d0117269a1 --- /dev/null +++ b/appengine-standard/getting-started/update/package.json @@ -0,0 +1,31 @@ +{ + "name": "appengine-standard-getting-started-update-apps", + "version": "0.0.1", + "description": "Node.js getting started sample for updating an app on App Engine standard environment.", + "private": true, + "main": "server.js", + "scripts": { + "lint": "samples lint", + "pretest": "npm run lint", + "test": "ava --verbose test/*.test.js" + }, + "repository": { + "type": "git", + "url": "https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git" + }, + "engines": { + "node": "8.x.x" + }, + "author": "Google Inc.", + "license": "Apache-2.0", + "dependencies": { + "body-parser": "^1.18.2", + "express": "^4.16.3", + "semistandard": "^12.0.1" + }, + "devDependencies": { + "@google-cloud/nodejs-repo-tools": "1.4.17", + "ava": "0.22.0", + "semistandard": "^12.0.1" + } +} diff --git a/appengine-standard/getting-started/update/server.js b/appengine-standard/getting-started/update/server.js new file mode 100644 index 0000000000..de0afd5a33 --- /dev/null +++ b/appengine-standard/getting-started/update/server.js @@ -0,0 +1,56 @@ +/** + * Copyright 2018, Google, Inc. + * 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. + */ + +'use strict'; + +// [START app] +const express = require('express'); +const bodyParser = require('body-parser'); +const path = require(`path`); + +const app = express(); + +// [START enable_parser] +app.use(bodyParser.urlencoded({ extended: true })); +// [END enable_parser] + +app.get('/', (req, res) => { + res.send('Hello from App Engine!'); +}); + +// [START add_display_form] +app.get('/submit', (req, res) => { + res.sendFile(path.join(__dirname, '/views/form.html')); +}); +// [END add_display_form] + +// [START add_post_handler] +app.post('/submit', (req, res) => { + console.log({ + name: req.body.name, + message: req.body.message + }); + res.send('Thanks for your message!'); +}); +// [END add_post_handler] + +// Listen to the App Engine-specified port, or 8080 otherwise +const PORT = process.env.PORT || 8080; +app.listen(PORT, () => { + console.log(`Server listening on port ${PORT}...`); +}); +// [END app] + +module.exports = app; diff --git a/appengine-standard/getting-started/update/test/server.test.js b/appengine-standard/getting-started/update/test/server.test.js new file mode 100644 index 0000000000..c35766abd0 --- /dev/null +++ b/appengine-standard/getting-started/update/test/server.test.js @@ -0,0 +1,59 @@ +// Copyright 2018, Google, Inc. +// 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. + +'use strict'; + +const path = require(`path`); +const test = require(`ava`); +const utils = require(`@google-cloud/nodejs-repo-tools`); + +const cwd = path.join(__dirname, `../`); +const requestObj = utils.getRequest({ + cwd: cwd, + cmd: `server` }); + +test.beforeEach(utils.stubConsole); +test.afterEach.always(utils.restoreConsole); + +test.cb.serial(`should send greetings`, (t) => { + requestObj + .get(`/`) + .expect(200) + .expect((response) => { + t.is(response.text, `Hello from App Engine!`); + }) + .end(t.end); +}); + +test.cb.serial(`should display form`, (t) => { + requestObj + .get(`/submit`) + .expect(200) + .expect((response) => { + t.regex(response.text, /textarea name="message" placeholder="Message"/); + }) + .end(t.end); +}); + +test.cb.serial(`should record message`, (t) => { + requestObj + .post(`/submit`, { + name: `sample-user`, + message: `sample-message` + }) + .expect(200) + .expect((response) => { + t.is(response.text, `Thanks for your message!`); + }) + .end(t.end); +}); diff --git a/appengine-standard/getting-started/update/views/form.html b/appengine-standard/getting-started/update/views/form.html new file mode 100644 index 0000000000..ad738456a3 --- /dev/null +++ b/appengine-standard/getting-started/update/views/form.html @@ -0,0 +1,20 @@ + + + + My App Engine App + + +

Create a new post

+
+
+ +
+
+ +
+
+ +
+
+ + \ No newline at end of file