Created
April 1, 2015 21:02
-
-
Save astemborskim/419d49f0f773fa623a90 to your computer and use it in GitHub Desktop.
Failure to redirect - using put method instead of Get
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 router = express.Router(); | |
var isAuthenticated = function (req, res, next) { | |
// if user is authenticated in the session, call the next() to call the next request handler | |
// Passport adds this method to request object. A middleware is allowed to add properties to | |
// request and response objects | |
if (req.isAuthenticated()) | |
return next(); | |
// if the user is not authenticated then redirect him to the login page | |
res.redirect('/'); | |
} | |
var isAdmin = function (req, res, next){ | |
if (req.user.isAdmin == true) | |
return next(); | |
req.flash('message','This action requires an admin account!'); | |
res.redirect('/home'); | |
} | |
module.exports = function(passport){ | |
/* GET login page. */ | |
router.get('/', function(req, res) { | |
// Display the Login page with any flash message, if any | |
res.render('index', { message: req.flash('message') }); | |
}); | |
/* Handle Login POST */ | |
router.post('/login', passport.authenticate('login', { | |
successRedirect: '/home', | |
failureRedirect: '/', | |
failureFlash : true | |
})); | |
/* GET Registration Page */ | |
router.get('/signup', function(req, res){ | |
res.render('register',{message: req.flash('message')}); | |
}); | |
/* Handle Registration POST */ | |
router.post('/signup', passport.authenticate('signup', { | |
successRedirect: '/home', | |
failureRedirect: '/signup', | |
failureFlash : true | |
})); | |
/* GET Home Page */ | |
router.get('/home', isAuthenticated, function(req, res){ | |
res.render('home', { user: req.user, message: req.flash('message')}); | |
}); | |
/* GET search Page */ | |
router.get('/search', isAuthenticated, function(req, res){ | |
res.render('search', { user: req.user, username : req.user.username, message: req.flash('message')}); | |
}); | |
/* GET search Page */ | |
router.get('/addProd', isAuthenticated, isAdmin, function(req, res){ | |
res.render('addProd', { user: req.user, message: req.flash('message')}); | |
}); | |
/* Handle Logout */ | |
router.get('/signout', function(req, res) { | |
req.logout(); | |
res.redirect('/'); | |
}); | |
return router; | |
} |
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
app.controller('productController', ['$scope', '$resource', function ($scope, $resource) { | |
$scope.prod={}; | |
$scope.edited={}; | |
$scope.prod.currentProd=null; | |
$scope.edited.inv=null; | |
var Inventory = $resource('/api/inventory/:id', {}, {update : {method : 'PUT'}}); | |
getInventory = function(req, res){ | |
Inventory.query(function (results){ | |
$scope.inventory=results; | |
//console.log(user); | |
})} | |
$scope.getProductBySKU = function (product){ | |
//console.log(product); | |
$scope.prod.currentProd=product; | |
$scope.prod.searchSKU.SKU = product.SKU; | |
$scope.prod.NameBySKU = product.Product_Name; | |
$scope.prod.DescBySKU = product.Product_Description; | |
$scope.prod.quantityBySKU = product.Quantity; | |
$scope.prod.locationBySKU = product.Product_Location; | |
$scope.prod.hideList = true; | |
$scope.edited = $scope.prod; | |
} | |
$scope.getProductByName = function (product){ | |
$scope.prod.currentProd=product; | |
$scope.prod.searchName.Product_Name = product.Product_Name; | |
$scope.prod.SKUByName = product.SKU; | |
$scope.prod.DescByName = product.Product_Description; | |
$scope.prod.quantityByName = product.Quantity; | |
$scope.prod.locationByName = product.Product_Location; | |
$scope.prod.hideList = true; | |
} | |
$scope.restoreProduct = function () { | |
//console.log(JSON.stringify($scope.prod.currentProd)); | |
$scope.prod.searchSKU.SKU = $scope.prod.currentProd.SKU; | |
$scope.prod.NameBySKU = $scope.prod.currentProd.Product_Name; | |
$scope.prod.DescBySKU = $scope.prod.currentProd.Product_Description; | |
$scope.prod.quantityBySKU = $scope.prod.currentProd.Quantity; | |
$scope.prod.locationBySKU = $scope.prod.currentProd.Product_Location; | |
$scope.prod.hideList = true; | |
} | |
$scope.setEdit=function(){ | |
$scope.edited.inv = $scope.prod.currentProd; | |
$scope.edited.SKU = $scope.prod.searchSKU.SKU; | |
$scope.edited.Name = $scope.prod.NameBySKU; | |
$scope.edited.Desc = $scope.prod.DescBySKU; | |
$scope.edited.quantity = $scope.prod.quantityBySKU; | |
$scope.edited.locations = $scope.prod.locationBySKU; | |
//console.log('setEdit:' + JSON.stringify($scope.edited.inv)); | |
} | |
$scope.updateProduct = function() { | |
//$scope.edited.inv = $scope.edited.searchSKU; | |
$scope.edited.inv.SKU = $scope.edited.SKU; | |
$scope.edited.inv.Product_Name = $scope.edited.Name; | |
$scope.edited.inv.Product_Description = $scope.edited.Desc; | |
$scope.edited.inv.Quantity = $scope.edited.quantity; | |
$scope.edited.inv.Product_Location = $scope.edited.locations; | |
//console.log('Edited: ' + JSON.stringify($scope.edited.inv)); | |
//console.log('Not Edited: ' + JSON.stringify($scope.prod.currentProd)); | |
var inventory = new Inventory($scope.edited.inv); | |
//console.log('edited.inv._id: ' + $scope.edited.inv._id); | |
inventory.$update({id : $scope.edited.inv._id}, inventory, function (err, results){ | |
if(err){}; | |
console.log(results); | |
}); | |
} | |
$scope.hideEdit = function(){ | |
$scope.prod.showEdit = false; | |
} | |
$scope.showEdit = function(){ | |
$scope.prod.showEdit = true; | |
} | |
$scope.showList = function(){ | |
$scope.prod.hideList = false; | |
} | |
$scope.orderBySKU = 'SKU'; | |
$scope.orderByName = 'Product_Name'; | |
$scope.hideEdit(); | |
getInventory(); | |
}]); |
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
extends layout | |
block navigation | |
div#navbar.navbar.navbar-default | |
ul.nav.nav-pills | |
li | |
a(href='/home') Home | |
li.active | |
a(href='/search') Product Search | |
li | |
a(href='/addProd') Add Inventory | |
li#user | |
| Signed in as: #{username} | |
li#signout | |
a(href="/signout") Sign Out | |
block heading | |
script(text="text/javascript", src="../js/controllers/product-controller.js") | |
block content | |
div#outerForm.panel.panel-primary(ng-controller="productController") | |
h4#mainHeading.panel.panel-heading(ng-hide="prod.showEdit==true") | |
| Product Search | |
h4#mainHeading.panel.panel-heading(ng-show="prod.showEdit==true") | |
| Edit Product | |
ul.nav.nav-tabs(ng-hide="prod.showEdit==true") | |
li(ng-class="{active:Search===SKU}") | |
a(ng-click="Search = SKU; loadSKUSearch()") SKU | |
li(ng-class="{active:Search===!SKU}") | |
a(ng-click="Search = ! SKU; loadNameSearch()") Product Name | |
div#innerForm.panel.panel-info(ng-show='Search === SKU') | |
h4.panel.panel-heading(ng-hide="prod.showEdit==true") | |
| Search by SKU: | |
h4.panel.panel-heading(ng-show="prod.showEdit==true") | |
| Edit: | |
div#formContainer.container-fluid | |
div#prodImg.column | |
img#productpic(class='thumbnail', src='./img/StockPhoto.jpg') | |
button#editProdButton.btn.btn-primary(ng-hide="prod.showEdit == true" ng-click='showEdit();setEdit()') Edit | |
div#prodForm.column | |
form#SKUSearch(class='form' autocomplete='off' ng-hide="prod.showEdit == true") | |
label SKU: | |
input(type='text', name='SKU', class='form-control', placeholder='SKU', ng-model='prod.searchSKU.SKU', ng-change="showList()", ng-click="showList()", required, autofocus) | |
div(ng-hide= "prod.searchSKU.SKU == null || prod.hideList == true") | |
div#prodList | |
table | |
tr | |
th SKU | |
th Product Name | |
tr(ng-repeat="prod in inventory | orderBy: orderBySKU | filter: prod.searchSKU") | |
td | |
a(href="" ng-click="getProductBySKU(prod)") {{prod.SKU}} | |
td {{prod.Product_Name}} | |
label Product Name: | |
input(type='text', ng-model="prod.NameBySKU" name='prodName', class='form-control nomargin', placeholder='Product Name', readonly) | |
label Product Description: | |
textarea#desc(type='text', ng-model="prod.DescBySKU" name='prodDesc', class='form-control', placeholder='Product Description',readonly) | |
label Quantity Available: | |
input(type='text', ng-model="prod.quantityBySKU" name='quantity', class='form-control', placeholder='Quantity Available',readonly) | |
label Location: | |
input(type='text', ng-model="prod.locationBySKU" name='location', class='form-control', placeholder='Location',readonly) | |
#message | |
if message | |
h2.text-center.error-message #{message} | |
span.clearfix | |
form#editSKUProd(class='form' autocomplete='off' ng-show="prod.showEdit == true") | |
label SKU: | |
input(type='text', ng-model='edited.SKU', name='SKU', class='form-control', placeholder='SKU') | |
label Product Name: | |
input(type='text', ng-model="edited.Name" name='prodName', class='form-control nomargin', placeholder='Product Name') | |
label Product Description: | |
textarea#desc(type='text', ng-model="edited.Desc" name='prodDesc', class='form-control', placeholder='Product Description') | |
label Quantity Available: | |
input(type='text', ng-model="edited.quantity" name='quantity', class='form-control', placeholder='Quantity Available') | |
label Location: | |
input(type='text', ng-model="edited.locations" name='location', class='form-control', placeholder='Location') | |
span.clearfix | |
button#editButtonGroup(class='btn btn-primary', type='button', ng-click="updateProduct(edited);hideEdit();restoreProduct()") Submit | |
button#editButtonGroup(class='btn btn-primary', type='button' ng-click="restoreProduct();hideEdit()") Cancel | |
div#searchNameForm.panel.panel-info(ng-show="Search === ! SKU") | |
h4.panel.panel-heading | |
| Search by Produt Name: | |
div#formContainer.container-fluid | |
div#prodImg.column | |
img#productpic(class='thumbnail', src='./img/StockPhoto.jpg') | |
button#editProdButton.btn.btn-primary | |
a#editProdLink(href='/home') Edit Item | |
div#prodForm | |
form#NameSearch(class='form' autocomplete='off') | |
label Product Name: | |
input(type='text', name='prodName', class='form-control', placeholder='Product Name', ng-model="prod.searchName.Product_Name", ng-change="showList()", ng-click="showList()", required, autofocus) | |
div(ng-hide="prod.searchName.Product_Name == null || prod.hideList == true") | |
div#prodList | |
table | |
tr | |
th SKU | |
th Product Name | |
tr(ng-repeat="prod in inventory | orderBy: orderByName | filter: prod.searchName") | |
td | |
a(href="" ng-click="getProductByName(prod)") {{prod.SKU}} | |
td {{prod.Product_Name}} | |
label SKU: | |
input(type='text', ng-model="prod.SKUByName" name='SKU', class='form-control', placeholder='SKU',required, readonly) | |
label Product Description: | |
textarea#desc(type='text', ng-model="prod.DescByName" name='prodDesc', class='form-control', placeholder='Product Description',readonly) | |
label Quantity Available: | |
input(type='text', ng-model="prod.quantityByName" name='quantity', class='form-control', placeholder='Quantity Available',readonly) | |
label Location: | |
input(type='text', ng-model="prod.locationByName" name='location', class='form-control', placeholder='Location',readonly) | |
//- button(class='btn btn-lg btn-primary btn-block', type='submit') Search | |
span.clearfix | |
block footer | |
p © Acme Warehouse Managment Systems 2015 |
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'), | |
mongoose = require('mongoose'), | |
dbConfig = require('./server/db/db.js'), | |
bodyparser = require('body-parser'), | |
cookieParser = require('cookie-parser'), | |
stylus = require('stylus'), | |
http = require('http'), | |
serverController = require('./server/controllers/server-controller'); | |
var app = express(); | |
//DB connection | |
mongoose.connect(dbConfig.url); | |
// mongoose.connect('mongodb://localhost:27017/anidopt'); | |
//View engine config | |
app.set('views', __dirname + '/client/views'); | |
app.set('view engine', 'jade'); | |
// app.use(defaultContentTypeMiddleware); | |
app.use(bodyparser.json()); | |
app.use(bodyparser.urlencoded({extended: true})); | |
app.use(cookieParser()); | |
app.use(express.static(__dirname + '/client/')); | |
//Configure Passport Authentication | |
var passport = require('passport'); | |
//Session Handling | |
var expressSession = require('express-session'); | |
app.use(expressSession({secret : 'mySecretKey'})); | |
app.use(passport.initialize()); | |
app.use(passport.session()); | |
// Using the flash middleware provided by connect-flash to store messages in session | |
// and displaying in templates | |
var flash = require('connect-flash'); | |
app.use(flash()); | |
// Initialize Passport | |
var initPassport = require('./server/passport/init'); | |
initPassport(passport); | |
//routes defined for passport authentication | |
var routes = require('./client/js/routes/index')(passport); | |
app.use('/', routes); | |
//REST API Routes | |
app.post('/api/addProd', serverController.addProduct); | |
app.get('/api/inventory', serverController.getInventory); | |
app.put('/api/inventory/:_id', serverController.editInventory); | |
//Start server on port 3000 | |
app.listen(3000, function () { | |
console.log("Server is listening..."); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment