Created
November 6, 2018 18:00
-
-
Save Msx1/25ebdc7cb93ddb26cd67de1e2afb2dd3 to your computer and use it in GitHub Desktop.
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 bodyParser = require('body-parser'); | |
| var path = require('path'); | |
| var expressValidator = require ('express-validator'); | |
| var mongojs = require('mongojs'); | |
| var db = mongojs('jstest', ['users']); | |
| var morgan = require('morgan') | |
| var ObjectId = mongojs.ObjectId; | |
| var app = express(); | |
| var router = express.Router(); | |
| app.use(morgan(':method :url :response-time')) | |
| var mongoose = require('mongoose'); | |
| mongoose.connect('mongodb://localhost/test'); | |
| // View Engine | |
| app.set('view engine', 'ejs'); | |
| app.set('views', path.join(__dirname, 'views')); | |
| app.get('/',function(req, res){//get,put,post,delete | |
| res.render('index') | |
| }); | |
| app.get('/reg', function(req, res){ | |
| res.render('reg') | |
| }); | |
| // body Parser Middleware | |
| app.use(bodyParser.json()); | |
| app.use(bodyParser.urlencoded({extended: false})); | |
| var bodyParser = require('body-parser'); | |
| app.use(bodyParser.json()); | |
| app.use(bodyParser.urlencoded({ extended: true })); | |
| // Set Static Path | |
| app.use(express.static(path.join(__dirname, 'public'))); | |
| // Global Vars | |
| app.use(function(req, res, next){ | |
| res.locals.errors = null; | |
| next(); | |
| }); | |
| // Express Validator Middleware | |
| app.use(expressValidator({ | |
| errorFormatter: function(param, msg, value) { | |
| var namespace = param.split('.') | |
| , root = namespace.shift() | |
| , formParam = root; | |
| while(namespace.length) { | |
| formParam += '[' + namespace.shift() + ']'; | |
| } | |
| return { | |
| param : formParam, | |
| msg : msg, | |
| value : value | |
| }; | |
| } | |
| })); | |
| app.get('/', function(req, res){ | |
| db.users.find(function (err, docs) { | |
| res.render('index', { | |
| title: 'Customers', | |
| users: docs | |
| }); | |
| }) | |
| }); | |
| app.post("/addname", (req, res) => { | |
| var myData = new User(req.body); | |
| myData.save() | |
| .then(item => { | |
| res.send("item saved to database"); | |
| }) | |
| .catch(err => { | |
| res.status(400).send("unable to save to database"); | |
| }); | |
| }); | |
| app.post('/users/add', function(req, res){ | |
| req.checkBody('first_name', 'First Name is Required').notEmpty(); | |
| req.checkBody('last_name', 'Last Name is Required').notEmpty(); | |
| req.checkBody('email', 'Email is Required').notEmpty(); | |
| var errors = req.validationErrors(); | |
| if (errors){ | |
| res.render('index', { | |
| title: 'Customers', | |
| users: users, | |
| errors: errors | |
| }) | |
| } else { | |
| var newUser = { | |
| first_name: req.body.first_name, | |
| last_name: req.body.last_name, | |
| email: req.body.email | |
| } | |
| db.users.insert(newUser, function(err, result){ | |
| if(err){ | |
| console.log(err); | |
| } | |
| res.redirect('/'); | |
| }); | |
| } | |
| }); | |
| app.delete('/users/delete/:id', function(req, res){ | |
| db.users.remove({_id: ObjectId(req.params.id)}, function(err, result){ | |
| if(err){ | |
| console.log(err); | |
| } | |
| res.redirect('/'); | |
| }); | |
| }); | |
| app.listen(3000, function(){ | |
| console.log('Server Started on Port 3000...'); | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment