Created
December 2, 2013 12:12
-
-
Save Juriy/7748601 to your computer and use it in GitHub Desktop.
Mailer example
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
var express = require('express'); | |
var nodemailer = require('nodemailer'); | |
var app = express(); | |
app.set('views', __dirname + '/views'); | |
app.set('view engine', 'ejs'); | |
app.use(function(err, req, res, next) { | |
console.log("ERROR " + err); | |
res.send("<b>Sorry, we're fixing that already"); | |
}); | |
app.use(function(req, res, next) { | |
console.log("Serving " + req.method + ":" | |
+ req.url); | |
if (req.url == "/err") { | |
foo.bar = 'Can\'t be!'; | |
} | |
next(); | |
}); | |
app.use(express.bodyParser()); | |
app.use(express.cookieParser()); | |
app.use(express.session({secret: "mysecret"})); | |
app.get("/ejs", function(req, res) { | |
res.render('list', { | |
names: ['john', 'pete'] | |
}); | |
}); | |
app.use(express.static(__dirname + "/public")); | |
// user/123 | |
app.get("/user", function(req, res) { | |
if (!req.session.user) { | |
res.send("No user in session"); | |
req.session.user = "PETE"; | |
} else { | |
res.send("You are " + req.session.user); | |
} | |
}); | |
app.use(function (err, req, resp, next) { | |
var transport = nodemailer.createTransport("SMTP", { | |
service: "Gmail", | |
auth: { | |
user: "nodecamp@gmail.com", | |
pass: "----" | |
} | |
}); | |
transport.sendMail({ | |
from : "nodecamp@gmail.com", // from | |
to : "juriy.bura@gmail.com", // to | |
subject : "Error report", // subject | |
body: "We got error here!\n" + err.stack // error description | |
}, function(error, responseStatus){ | |
if(!error){ | |
console.log(responseStatus.message); // response from the server | |
} else { | |
console.log("ERROR " + error); | |
} | |
}); | |
next(err); | |
} | |
); | |
app.listen(80); | |
console.log("listening 80"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment