|
| 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; |
0 commit comments