Skeleton MVC Crud Node Js + Express Js + MongoDB - 2da Parte
/* begin /bin/www */
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('project-express:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
/* end /bin/www */
/* --------------------------------- */
/* begin /controllers/ProductController.js */
var mongoose = require('mongoose');
var Product = require("../models/Product");
var productController = {};
productController.list = function(req, res){
Product.find({}).exec(function(err, products){
if( err ){ console.log('Error: ', err); return; }
console.log("The INDEX");
res.render('../views/product/index', {products: products} );
});
};
productController.show = function(req, res){
Product.findOne({_id: req.params.id}).exec(function(err, product){
if( err ){ console.log('Error: ', err); return; }
res.render('../views/product/show', {product: product} );
});
};
productController.create = function(req, res){
res.render('../views/product/create');
};
productController.save = function(req, res){
var product = new Product( req.body );
product.save(function(err){
if( err ){ console.log('Error: ', err); return; }
console.log("Successfully created a product. :)");
res.redirect("/products/show/"+product._id);
});
};
productController.edit = function(req, res) {
Product.findOne({_id: req.params.id}).exec(function (err, product) {
if (err) { console.log("Error:", err); return; }
res.render("../views/product/edit", {product: product});
});
};
productController.update = function(req, res){
Product.findByIdAndUpdate( req.params.id, {$set: {
name: req.body.name,
price: req.body.price
}}, { new: true },
function( err, product){
if( err ){
console.log('Error: ', err);
res.render('../views/product/edit', {product: req.body} );
}
console.log( product );
res.redirect('/products/show/' + product._id);
});
};
productController.delete = function(req, res){
Product.remove({_id: req.params.id}, function(err){
if( err ){ console.log('Error: ', err); return; }
console.log("Product deleted!");
res.redirect("/products");
});
};
module.exports = productController;
/* end /controllers/ProductController.js */
Comentarios
Publicar un comentario