v0.0.6 Refactor - zhentian-wan/MEANAppsFiles GitHub Wiki
0.0.6 -- Refactor
As the app growing, we want to get rid of the partial folder. And put the partials files along side with its related javascript files. This should be the same idea as module-approach-structure for AngularJS application.
But this cause some problems:
In server.js we set the routes as:
app.get('/partials/:partialPath', function(req,res) {
res.render('partials/' + req.params.partialPath);
});
app.get('*', function(req,res) {
res.render('index');
});
Because partials files are no long there, they are inside public/app dir and we use (app/main/.jade) / (app/account/.jade), so we change to:
app.get('/partials/*', function(req,res) {
res.render('../../public/app/' + req.params[0]);
});
app.get('*', function(req,res) {
res.render('index');
});
Server.js
We want serrate different module into its own file, keep server.js clean:
server.js:
var express = require('express');
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var app = new express();
var config = require('./server/config/config')[env];
require('./server/config/express')(app, config);
require('./server/config/mongoose')(config);
require('./server/config/routes')(app);
app.listen(config.port);
console.log("Server is listening at " + config.port);
config/config.js
var path = require('path'),
rootPath = path.normalize(__dirname + "../../../");
//We can pass in param to set development or production mode dynamiclly
module.exports = {
development: {
rootPath: rootPath,
db: 'mongodb://localhost/multivision',
port: process.env.PORT || 3030
},
production: {
rootPath: rootPath,
db: 'mongodb://zhentian:[email protected]:31932/multivision',
port: process.env.PORT || 80
}
};
Required by:
var config = require('./server/config/config')[env];
config/express.js
var express = require('express'),
stylus = require('stylus'),
nib = require('nib'),
logger = require('morgan'),
bodyParser = require('body-parser');
module.exports = function(app, config) {
//compile stylus
function compile(str, path) {
return stylus(str)
.set('filename', path)
.set('compress', true)
.use(nib())
.import('nib');
}
// Set view page path
app.set('views', config.rootPath + '/server/views');
// Set jade
app.set('view engine', 'jade');
//logging
app.use(logger('dev'));
//bodyparser
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//Use stylus
app.use('/css', stylus.middleware(
{
src: config.rootPath + '/public'
, compile: compile
}
));
//Any request comes in, will find the same filename file in public dir
app.use(express.static( config.rootPath + '/public'));
};
config/mongoose.js
var mongoose = require('mongoose');
module.exports = function(config){
//Use Mongoose
mongoose.connect(config.db);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error'));
db.once('open', function callback(){
console.log('db opened');
});
}
config/routes.js
module.exports = function(app){
//Set route for partials
//When request comes for main partials, it will look for server/views/partials/mian
app.get('/partials/*', function(req,res) {
res.render('../../public/app/' + req.params[0]);
});
// All routes handled by this route, give client side to handle
app.get('*', function(req,res) {
res.render('index');
});
}