Skip to content

Commit a450f8a

Browse files
committed
Added sendgrid example app. Also used for Compute Engine snippet.
1 parent ba9b489 commit a450f8a

File tree

13 files changed

+300
-2
lines changed

13 files changed

+300
-2
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ npm-debug.log
33
coverage/
44

55
test/encrypted/nodejs-docs-samples.json
6-
dump.rdb
6+
dump.rdb
7+
logs/
8+
*.iml
9+
.idea/

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ and applications on [Google App Engine](http://cloud.google.com/nodejs).
3333
- Bower - [Source code][bower_1] | [App Engine Tutorial][bower_2] | [Documentation][bower_3]
3434
- Grunt - [Source code][grunt_1] | [App Engine Tutorial][grunt_2] | [Live demo][grunt_3] | [Documentation][grunt_4]
3535
- Mailgun - [Source code][mailgun_1] | [App Engine Tutorial][mailgun_2] | [Documentation][mailgun_3]
36+
- Sendgrid - [Source code][sendgrid_1] | [App Engine Tutorial][sendgrid_2] | [Documentation][sendgrid_3]
3637
- Webpack - [Source code][webpack_1] | [App Engine Tutorial][webpack_2] | [Documentation][webpack_3]
3738

3839
## Google Storage
@@ -134,6 +135,10 @@ See [LICENSE](https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/ma
134135
[mailgun_2]: https://cloud.google.com/nodejs/resources/tools/mailgun
135136
[mailgun_3]: http://www.mailgun.com/
136137

138+
[sendgrid_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/sendgrid
139+
[sendgrid_2]: https://cloud.google.com/nodejs/resources/tools/sendgrid
140+
[sendgrid_3]: http://sendgrid.com/
141+
137142
[webpack_1]: https://github.com/GoogleCloudPlatform/nodejs-docs-samples/blob/master/appengine/webpack
138143
[webpack_2]: https://cloud.google.com/nodejs/resources/tools/webpack
139144
[webpack_3]: https://webpack.github.io/

appengine/sendgrid/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Express.js + Sendgrid on Google App Engine
2+
3+
> [Sendgrid][1]: Delivering your transactional and marketing email through one reliable platform.
4+
>
5+
> – sendgrid.com
6+
7+
This sample application demonstrates how to use [Express.js][2] and
8+
[sendgrid-nodejs][3] to send transactional email on [Google App Engine][4].
9+
10+
Read the [Sendgrid on App Engine Tutorial][5] for how to run and deploy this
11+
sample app.
12+
13+
You can also read the [Sendgrid documentation][6].
14+
15+
[1]: https://sendgrid.com/
16+
[2]: http://expressjs.com
17+
[3]: https://github.com/sendgrid/sendgrid-nodejs
18+
[4]: https://cloud.google.com/appengine
19+
[5]: https://cloud.google.com/nodejs/resources/tools/sendgrid
20+
[6]: https://sendgrid.com/docs

appengine/sendgrid/app.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2015, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var express = require('express');
17+
var path = require('path');
18+
var bodyParser = require('body-parser');
19+
20+
// [START setup]
21+
var Sendgrid = require('sendgrid')(process.env.SENDGRID_API_KEY);
22+
// [END setup]
23+
24+
var app = express();
25+
26+
// Setup view engine
27+
app.set('views', path.join(__dirname, 'views'));
28+
app.set('view engine', 'jade');
29+
30+
// Parse form data
31+
app.use(bodyParser.urlencoded({ extended: false }));
32+
33+
// [START index]
34+
app.get('/', function(req, res) {
35+
res.render('index');
36+
});
37+
// [END index]
38+
39+
// [START hello]
40+
app.post('/hello', function(req, res, next) {
41+
Sendgrid.send({
42+
from: '[email protected]', // From address
43+
to: req.body.email, // To address
44+
subject: 'Hello World!', // Subject
45+
text: 'Sendgrid on Google App Engine with Node.js', // Content
46+
}, function (err) {
47+
if (err) {
48+
return next(err);
49+
}
50+
// Render the index route on success
51+
return res.render('index', {
52+
sent: true
53+
});
54+
});
55+
});
56+
// [END hello]
57+
58+
if (module === require.main) {
59+
// [START server]
60+
var server = app.listen(
61+
process.env.PORT || 8080,
62+
'0.0.0.0',
63+
function () {
64+
var address = server.address().address;
65+
var port = server.address().port;
66+
console.log('App listening at http://%s:%s', address, port);
67+
console.log('Press Ctrl+C to quit.');
68+
}
69+
);
70+
// [END server]
71+
}
72+
73+
module.exports = app;

appengine/sendgrid/app.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
#
14+
# [START app_yaml]
15+
runtime: nodejs
16+
vm: true
17+
env_variables:
18+
SENDGRID_API_KEY: <your-sendgrid-api-key>
19+
# [END app_yaml]
20+
skip_files:
21+
- ^(.*/)?.*/node_modules/.*$

appengine/sendgrid/package.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "appengine-sendgrid",
3+
"description": "An example of using Sendgrid in Node.js on Google App Engine.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"engines": {
8+
"node": "~4.2"
9+
},
10+
"scripts": {
11+
"start": "node app.js",
12+
"deploy": "gcloud preview app deploy app.yaml"
13+
},
14+
"dependencies": {
15+
"body-parser": "^1.14.1",
16+
"express": "^4.13.3",
17+
"jade": "^1.11.0",
18+
"sendgrid": "^2.0.0"
19+
}
20+
}

appengine/sendgrid/views/index.jade

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2015, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
doctype html
15+
html
16+
head
17+
title= title
18+
body
19+
h1 Hello World!
20+
p Express.js + Sendgrid on Google App Engine.
21+
hr
22+
if sent
23+
p Email sent!
24+
else
25+
form(name="hello", action="/hello", method="post")
26+
input(type="email", placeholder="Enter your email to send yourself a Hello World message", name="email", style="width: 50%; margin-right: 15px;")
27+
input(type="submit", value="Send")

computeengine/sendgrid/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "computeengine-sendgrid",
3+
"description": "An example of sending an email with Sendgrid in Node.js on Google Compute Engine.",
4+
"version": "0.0.1",
5+
"private": true,
6+
"license": "Apache Version 2.0",
7+
"engines": {
8+
"node": "~4.2"
9+
},
10+
"dependencies": {
11+
"sendgrid": "^2.0.0"
12+
}
13+
}

computeengine/sendgrid/sendmail.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright 2015, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
// [START send]
17+
var Sendgrid = require('sendgrid')(
18+
process.env.SENDGRID_API_KEY || '<your-sendgrid-api-key>'
19+
);
20+
21+
Sendgrid.send({
22+
from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM', // From address
23+
to: '[email protected]', // To address
24+
subject: 'test email from Node.js on Google Cloud Platform', // Subject
25+
text: 'Hello!\n\nThis a test email from Node.js.' // Content
26+
}, function (err, json) {
27+
if (err) {
28+
console.log(err);
29+
} else {
30+
console.log(json);
31+
}
32+
});
33+
// [END send]

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@
2727
"deps_datastore": "cd datastore && npm i && cd ../..",
2828
"deps_storage": "cd storage && npm i && cd ../..",
2929
"deps_express": "cd appengine/express && npm i && cd ../..",
30+
"deps_sendgrid": "cd appengine/sendgrid && npm i && cd ../.. && cd computeengine/sendgrid && npm i && cd ../..",
3031
"deps_memcached": "cd appengine/express-memcached-session && npm i && cd ../..",
3132
"pretest_geddy": "cd appengine/geddy && npm i geddy; GEDDY_SECRET=config/secrets.json; [[ -f $GEDDY_SECRET ]] || echo '{}' > $GEDDY_SECRET && node node_modules/.bin/geddy gen secret; cd ../..;",
32-
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_memcached && npm run deps_express && npm run pretest_geddy",
33+
"pretest": "npm run deps_datastore && npm run deps_storage && npm run deps_memcached && npm run deps_express && npm run deps_sendgrid && npm run pretest_geddy",
3334
"test": "npm run jshint && npm run cover"
3435
},
3536
"devDependencies": {
@@ -39,6 +40,7 @@
3940
"istanbul": "^0.4.0",
4041
"jshint": "~2.8.0",
4142
"mocha": "^2.2.5",
43+
"proxyquire": "^1.7.3",
4244
"request": "^2.65.0",
4345
"supertest": "^1.1.0"
4446
}

test/appengine/all.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,12 @@ var sampleTests = [
9999
args: ['server.js'],
100100
msg: 'Hello World! Restify.js on Google App Engine.'
101101
},
102+
{
103+
dir: 'sendgrid',
104+
cmd: 'node',
105+
args: ['app.js'],
106+
msg: 'Express.js + Sendgrid on Google App Engine.'
107+
},
102108
{
103109
dir: 'webpack',
104110
cmd: 'node',

test/appengine/sendgrid.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// Copyright 2015, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var assert = require('assert');
17+
var request = require('supertest');
18+
var app = require('../../appengine/sendgrid/app.js');
19+
20+
describe('sendgrid', function () {
21+
it('should return 200 and a "Hello World" message', function (done) {
22+
var message = 'Express.js + Sendgrid on Google App Engine';
23+
24+
request(app)
25+
.get('/')
26+
.expect(200)
27+
.expect(function (res) {
28+
assert(
29+
res.text.indexOf(message) !== -1,
30+
'Response should contain a "Hello World" message.\n' +
31+
'Found: ' + res.text
32+
);
33+
})
34+
.end(done);
35+
});
36+
});

test/computeengine/sendgrid.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2015, Google, Inc.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
'use strict';
15+
16+
var assert = require('assert');
17+
var proxyquire = require('proxyquire').noPreserveCache();
18+
process.env.SENDGRID_API_KEY = 'foo';
19+
20+
describe('sendgrid', function () {
21+
it('should send an email', function (done) {
22+
proxyquire('../../computeengine/sendgrid/sendmail.js', {
23+
sendgrid: function (key) {
24+
assert.equal(key, 'foo');
25+
return {
26+
send: function (payload) {
27+
assert.deepEqual(payload, {
28+
from: 'ANOTHER_EMAIL@ANOTHER_EXAMPLE.COM',
29+
30+
subject: 'test email from Node.js on Google Cloud Platform',
31+
text: 'Hello!\n\nThis a test email from Node.js.'
32+
});
33+
done();
34+
}
35+
};
36+
}
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)