-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.min.js
1 lines (1 loc) · 1.34 KB
/
server.min.js
1
require("dotenv").config();const express=require("express"),mongoose=require("mongoose"),path=require("path");var port=process.env.PORT||3e3,bodyParser=require("body-parser");const{body:body,validationResult:validationResult}=require("express-validator"),app=express();app.use(express.static(__dirname)),app.use(express.urlencoded({extended:!0})),mongoose.connect(process.env.MONGO_URI);const db=mongoose.connection;db.on("error",(error=>{console.error("MongoDB connection error:",error)})),db.once("open",(()=>{console.log("Mongodb connection is a go!")}));const userSchema=new mongoose.Schema({name:String,email:String,comments:String}),Users=mongoose.model("data",userSchema);app.get("/",((req,res)=>{res.sendFile(path.join(__dirname," ../index.html"))})),app.post("/post",body("name").trim().notEmpty().isLength({min:3,max:30}),body("email").isEmail(),(async(req,res)=>{const{name:name,email:email,comments:comments}=req.body,user=new Users({name:name,email:email,comments:comments});if(!validationResult(req).isEmpty())return res.status(400).send("<h4 style = 'font-family: Arial, Helvetica, sans-serif;'>No empty fields. Name should be more than 2 characters long<a href = 'form.html' class = 'button'><br>Back</a></h4>");await user.save(),console.log(user),res.redirect("signup_success.html")})),app.listen(port,(()=>{console.log("Server started port : "+port)}));