Last active
November 22, 2017 15:39
-
-
Save fed/83ca2cd757525bd027e8 to your computer and use it in GitHub Desktop.
Sending emails with Express.js and Node.js
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
{ | |
"name": "serverify", | |
"version": "0.0.0", | |
"description": "Simple web server powered by Node.js, Express.js and Nodemailer", | |
"main": "server.js", | |
"scripts": { | |
"start": "node server.js", | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"dependencies": { | |
"body-parser": "^1.14.1", | |
"compression": "^1.6.0", | |
"express": "^4.13.3", | |
"nodemailer": "^1.8.0" | |
} | |
} |
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 compression = require('compression'); | |
var bodyParser = require('body-parser'); | |
var nodemailer = require('nodemailer'); | |
var app = express(); | |
var transporter = nodemailer.createTransport(); | |
app.use(compression()); | |
app.use(express.static(__dirname + '/dist/')); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.get('/', function (req, res) { | |
res.sendFile(__dirname + '/dist/index.html'); | |
}); | |
app.post('/contact-us', function (req, res) { | |
transporter.sendMail({ | |
from: '[email protected]', | |
to: '[email protected]', | |
subject: 'Contact Form', | |
html: 'Name: ' + req.body.name + '<br>Email: ' + req.body.email + '<br>Message: ' + req.body.message | |
}, function (error, info) { | |
res.json({ | |
status: error ? 'fail' : 'success' | |
}); | |
}); | |
}); | |
app.use(function(req, res, next) { | |
res.sendFile(__dirname + '/dist/index.html'); | |
}); | |
app.listen(process.env.PORT || 80); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment