Last active
December 13, 2017 12:01
-
-
Save umarmw/15baf0b7f151f228829aafb73a967cfd to your computer and use it in GitHub Desktop.
contact form with express js example
This file contains hidden or 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 Webtask = require('webtask-tools'); | |
var bodyParser = require('body-parser'); | |
var app = express(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({ extended: true })); | |
app.get('/', function (req, res) { | |
res.sendStatus(200); | |
}); | |
app.post('/contact', function (req, res) { | |
var name = req.body.name; | |
var email = req.body.email; | |
var message = req.body.message; | |
var contact = req.body.contact; | |
var type = "other"; | |
var msg = ""; | |
if (name === "") { | |
type = "error"; | |
msg += " Enter a name! "; | |
} | |
if (email === "") { | |
type = "error"; | |
msg += " Enter an email! "; | |
} | |
if (message === "") { | |
type = "error"; | |
msg += " Enter a message! "; | |
} | |
if (contact === "") { | |
type = "error"; | |
msg += " Enter a contact! "; | |
} | |
if(type !== "error"){ | |
type = "success"; | |
msg += " Contact sent successful! "; | |
} | |
res.json({"type": type, "message": msg}); | |
}); | |
module.exports = Webtask.fromExpress(app); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment