-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
55 lines (45 loc) · 1.27 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//jshint esversion:6
const express = require("express");
const path = require("path");
const bodyParser = require("body-parser");
const mongodb = require("mongodb");
const mongoose = require("mongoose");
const app = express();
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
mongoose.connect("mongodb+srv://admin-arjun:Test123@cluster0.vipcp.mongodb.net/formDB", { useNewUrlParser: true });
const formSchema = new mongoose.Schema({
name: String,
email: String,
mobile: Number,
message: String
});
const Form = mongoose.model("Form", formSchema);
app.get("/", function(req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res) {
const form = new Form({
name: req.body.name,
email: req.body.email,
mobile: req.body.mobile,
message: req.body.message
});
form.save(function(err) {
if (!err) {
console.log("Data saved successfully!");
} else {
console.log(err);
}
});
setTimeout(function() {
res.redirect("/");
}, 1000);
});
let port = process.env.PORT;
if (port == null || port == "") {
port = 3000;
}
app.listen(port, function() {
console.log("Server working on Port: 3000");
});