Permalink
Browse files

Fix routes

  • Loading branch information...
1 parent 183cd9f commit b9c8a4dcfb496fd370594e7faeb3a78cdacee03d @biggora committed Dec 24, 2012
View
23 app.js
@@ -8,7 +8,8 @@ engine = require('ejs-locals'),
useragent = require('express-useragent'),
sessionStore = require("./utils/session")(express),
helper = require('./utils/helper'),
-flash = require('./utils/flash');
+flash = require('./utils/flash'),
+config = require('./config');
var curpath = __dirname.replace(/\\/gi,"/"); // Fix for Windows
var app;
@@ -45,20 +46,20 @@ function bootApplication(app) {
app.configure(function () {
app.use(express.compress());
- app.use(express.methodOverride());
app.use(express.bodyParser({
uploadDir: curpath + '/uploads',
keepExtensions: true,
- encoding: 'utf-8'
+ encoding: config.parser.encoding
}));
- app.use(express.cookieParser('secret'));
+ app.use(express.methodOverride());
+ app.use(express.cookieParser(config.session.secret));
app.use(express.session({
cookie: {
- maxAge: 8640000
+ maxAge: config.session.maxAge
},
- key: 'trinte',
- secret: 'secret',
- store: new sessionStore({ url : app.set('db-uri') })
+ key: config.session.key,
+ secret: config.session.secret,
+ store: new sessionStore({ url : config.dburi() })
}));
app.use(flash());
app.use(express.csrf());
@@ -67,7 +68,7 @@ function bootApplication(app) {
app.use(express.static(curpath + '/public'));
// Setup ejs views as default, with .html as the extension
- app.set('port', process.env.PORT || 3000);
+ app.set('port', process.env.PORT || config.port);
app.set('views', curpath + '/views');
app.engine('html', engine);
app.set('view engine', 'html');
@@ -90,7 +91,6 @@ function bootApplication(app) {
app.use(function (req, res) {
res.render('404');
});
-
});
}
@@ -103,7 +103,7 @@ function bootModels(app) {
});
});
// Connect to mongoose
- mongoose.connect(app.set('db-uri'));
+ mongoose.connect(config.dburi());
}
// Bootstrap controllers
@@ -121,7 +121,6 @@ function bootControllers(app) {
function bootModel(app, file) {
var name = file.replace('.js', ''),
schema = require(curpath + '/models/' + name); // Include the mongoose file
-
}
// Load the controller, link to its view file from here
View
@@ -5,11 +5,12 @@
"author": "",
"private": true,
"dependencies": {
- "ejs-locals" : ">=1.0.2",
- "express" : ">=3.0.0",
- "mongoose" : ">=3.0.0",
- "socket.io" : ">=0.8.0",
+ "ejs-locals" : ">=1.0.2",
+ "express" : ">=3.0.0",
+ "mongodb" : ">=1.2.6",
+ "mongoose" : ">=3.0.0",
+ "socket.io" : ">=0.8.0",
"express-useragent" : ">=0.0.7",
- "trinte" : ">0.0.4"
+ "trinte" : ">0.0.4"
}
}
View
@@ -242,6 +242,7 @@ function createApplicationAt(path,appname) {
copy(bootstrapPath + '/app-cluster.js', appdir + '/app-cluster.js');
copy(bootstrapPath + '/app.js', appdir + '/app.js');
+ copy(bootstrapPath + '/config.js', appdir + '/config.js');
var layout = fs.readFileSync(bootstrapPath + '/views/layout.html', 'utf-8');
write(appdir + '/views/layout.html', layout.toString().replace(/APPNAME/gi,appname));
View
@@ -2,7 +2,6 @@
* DEVELOPMENT Environment settings
*/
module.exports = function(app,express) {
- app.set('db-uri', 'mongodb://localhost/trinte-development');
app.use(express.logger("dev"));
app.use(express.errorHandler({
dumpExceptions: true,
View
@@ -3,7 +3,6 @@
* TEST Environment settings
*/
module.exports = function(app,express) {
- app.set('db-uri', 'mongodb://localhost/trinte-production');
app.use(express.logger({
format: ':method :url :status'
}));
View
@@ -3,7 +3,6 @@
* PRODUCTION Environment settings
*/
module.exports = function(app,express) {
- app.set('db-uri', 'mongodb://localhost/trinte-production');
app.use(express.logger({
format: ':method :url :status'
}));
@@ -12,5 +11,4 @@ module.exports = function(app,express) {
dumpExceptions: false,
showStack: false
}));
-
}
View
@@ -0,0 +1,31 @@
+module.exports = {
+ port : 3000,
+ session: {
+ maxAge : 8640000,
+ key : "trinte",
+ secret : "Web-based Application"
+ },
+ parser : {
+ encoding : "utf-8",
+ keepExtensions : true
+ },
+ db: {
+ host : "localhost",
+ port : "27017",
+ username : "",
+ password : "",
+ database : "trinte-dev"
+ },
+ dburi: function() {
+ var uri = "mongodb://", port = 27017;
+ if(this.db.username != "" && this.db.password != "") {
+ uri += this.db.username + ':' + this.db.password + '@';
+ }
+ if(this.db.port != "") {
+ port = this.db.port;
+ }
+ uri += this.db.host + ':' + port;
+ uri += '/' + this.db.database
+ return uri;
+ }
+}
@@ -1,29 +1,12 @@
var fs = require('fs')
- , inflection = require('../lib/inflection');
+, inflection = require('../lib/inflection');
-module.exports = function (app) {
-
- // app.get("/favicon.ico", function() {}); // Required if you delete the favicon.ico from public
-
- // Plural
- app.get("/:controller?", router); // Index
- app.get("/:controller.:format?", router); // Index
- app.get("/:controller/:from-:to.:format?", router); // Index
-
- // Plural Create & Delete
- app.post("/:controller", router); // Create
- app.del("/:controller", router); // Delete all
-
- // Singular - different variable to clarify routing
- app.get("/:controller/:action", router); // Add (New)
- app.get("/:controller/:id.:format?", router); // To support controller/index
- app.get("/:controller/:id/:action", router); // Show edit
- app.put("/:controller/:id", router); // Update
- app.del("/:controller/:id", router); // Delete
-
-}
-
-///
+/*
+ * Default Application router.
+ * @param {Object} req
+ * @param {Object} res
+ * @param {Function} next
+ */
function router(req, res, next) {
var controller = req.params.controller ? req.params.controller : '';
@@ -91,12 +74,12 @@ function router(req, res, next) {
}
};
-
/**
* Default Application index - shows a list of the controllers.
* Redirect here if you prefer another controller to be your index.
- * @param req
- * @param res
+ * @param {Object} req
+ * @param {Object} res
+ * @param {Function} next
*/
function index(req, res, next) {
@@ -121,3 +104,26 @@ function index(req, res, next) {
});
});
};
+
+
+module.exports = function (app) {
+
+ // app.get("/favicon.ico", function() {}); // Required if you delete the favicon.ico from public
+
+ // Plural
+ app.get("/:controller?", router); // Index
+ app.get("/:controller.:format?", router); // Index
+ app.get("/:controller/:from-:to.:format?", router); // Index
+
+ // Plural Create & Delete
+ app.post("/:controller", router); // Create
+ app.del("/:controller", router); // Delete all
+
+ // Singular - different variable to clarify routing
+ app.get("/:controller/:action", router); // Add (New)
+ app.get("/:controller/:id.:format?", router); // To support controller/index
+ app.get("/:controller/:id/:action", router); // Show edit
+ app.put("/:controller/:id", router); // Update
+ app.del("/:controller/:id", router); // Delete
+
+}
View
@@ -14,10 +14,10 @@ module.exports = {
},
/**
- * Index action, returns a list either via the views/examples/index.html view or via json
- * Default mapping to GET '/examples'
- * For JSON use '/examples.json'
- **/
+ * Index action, returns a list either via the views/examples/index.html view or via json
+ * Default mapping to GET '/examples'
+ * For JSON use '/examples.json'
+ **/
index: function(req, res, next) {
var from = req.params.from ? parseInt(req.params.from) - 1 : 0;
@@ -53,10 +53,10 @@ module.exports = {
},
/**
- * Show action, returns shows a single item via views/examples/show.html view or via json
- * Default mapping to GET '/example/:id'
- * For JSON use '/example/:id.json'
- **/
+ * Show action, returns shows a single item via views/examples/show.html view or via json
+ * Default mapping to GET '/example/:id'
+ * For JSON use '/example/:id.json'
+ **/
show: function(req, res, next) {
Example.findById(req.params.id, function(err, example) {
@@ -77,9 +77,9 @@ module.exports = {
},
/**
- * Edit action, returns a form via views/examples/edit.html view no JSON view.
- * Default mapping to GET '/example/:id/edit'
- **/
+ * Edit action, returns a form via views/examples/edit.html view no JSON view.
+ * Default mapping to GET '/example/:id/edit'
+ **/
edit: function(req, res, next){
Example.findById(req.params.id, function(err, example) {
if(err) return next(err);
@@ -90,9 +90,9 @@ module.exports = {
},
/**
- * Update action, updates a single item and redirects to Show or returns the object as json
- * Default mapping to PUT '/example/:id', no GET mapping
- **/
+ * Update action, updates a single item and redirects to Show or returns the object as json
+ * Default mapping to PUT '/example/:id', no GET mapping
+ **/
update: function(req, res, next){
Example.findById(req.params.id, function(err, example) {
@@ -120,9 +120,9 @@ module.exports = {
},
/**
- * Create action, creates a single item and redirects to Show or returns the object as json
- * Default mapping to POST '/examples', no GET mapping
- **/
+ * Create action, creates a single item and redirects to Show or returns the object as json
+ * Default mapping to POST '/examples', no GET mapping
+ **/
create: function(req, res, next){
var example = new Example(req.body.example);
@@ -149,9 +149,9 @@ module.exports = {
},
/**
- * Delete action, deletes a single item and redirects to index
- * Default mapping to DEL '/example/:id', no GET mapping
- **/
+ * Delete action, deletes a single item and redirects to index
+ * Default mapping to DEL '/example/:id', no GET mapping
+ **/
destroy: function(req, res, next){
Example.findById(req.params.id, function(err, example) {
@@ -172,7 +172,5 @@ module.exports = {
}
});
});
-
}
-
};
View
@@ -11,7 +11,7 @@
"express",
"mongodb"
],
- "version": "0.0.4-7",
+ "version": "0.0.5",
"homepage": "https://github.com/biggora/trinte",
"repository": {
"type": "git",
@@ -20,16 +20,18 @@
"main": "trinte",
"author": "Aleksej Gordejev <aleksej@gordejev.lv> (https://github.com/biggora/)",
"engines": {
- "node": ">=0.8"
+ "node": ">=0.8",
+ "npm": ">=1.0"
},
"dependencies": {
"should": ">=1.2.0",
- "wrench": ">=1.4.4"
+ "wrench": ">=1.4.4",
+ "ejs": ">=0.8.0",
+ "mongodb": ">=1.2.6",
},
"devDependencies": {
"express": ">=3.0.0",
"connect": ">=2.0.0",
- "ejs": ">=0.8.0",
"socket.io": ">=0.8.0",
"mongoose": ">=3.0.0"
},
@@ -51,13 +51,31 @@ exports.execute = function(params,appPath) {
}
}
+ var projectdata = {
+ name: "",
+ description: "",
+ version: "",
+ author: ""
+ };
+ if (fs.existsSync(appPath + '/package.json')) {
+ var pd = fs.readFileSync(appPath + '/package.json', "utf-8");
+ try {
+ projectdata = JSON.parse(pd);
+ } catch (err) {
+ }
+ }
// Read the template
var str = fs.readFileSync(controllerTemplate, 'utf8');
// Render the model
var ret = ejs.render(str, {
locals: {
+ pack: projectdata.name,
+ description: projectdata.description,
+ version: projectdata.version,
+ author: projectdata.author,
+ created: new Date().toISOString(),
controllerName:controllerName,
modelName:modelName
},
Oops, something went wrong.

0 comments on commit b9c8a4d

Please sign in to comment.