Skip to content

Instantly share code, notes, and snippets.

@superlou
Created March 4, 2012 23:53
Show Gist options
  • Save superlou/1975480 to your computer and use it in GitHub Desktop.
Save superlou/1975480 to your computer and use it in GitHub Desktop.
// ============================================================================
// TASKS
// ============================================================================
task.registerTask("server", "Run development server.", function(prop) {
var props = ["server"];
// If a prop was passed as the argument, use that sub-property of server.
if (prop) { props.push(prop); }
var options = config(props) || {};
// Defaults set for server values
var options = underscore.defaults(options, {
favicon: "./favicon.ico",
index: "./index.html",
port: 8000,
host: "127.0.0.1"
});
options.folders = options.folders || {};
// Ensure folders have correct defaults
options.folders = underscore.defaults(options.folders, {
app: "./app",
assets: "./assets",
dist: "./dist"
});
options.files = options.files || {};
// Ensure files have correct defaults
options.files = underscore.defaults(options.files, {
"app/config.js": "app/config.js"
});
// Run the server
task.helper("server", options);
// Fail task if errors were logged
if (task.hadErrors()) { return false; }
log.writeln("Listening on http://" + options.host + ":" + options.port);
});
// ============================================================================
// HELPERS
// ============================================================================
task.registerHelper("server", function(options) {
// Require libraries
var fs = require("fs");
var express = require("express");
var site = express.createServer();
site.use(express.bodyParser());
// Set up Mongoose
var mongoose = require('mongoose');
var mongoose_config = require('./mongoose_config');
var Profile = mongoose_config.Profile(mongoose);
mongoose.connect('mongodb://localhost/connie');
// Map static folders
Object.keys(options.folders).sort().reverse().forEach(function(key) {
site.use("/" + key, express.static(options.folders[key]));
});
// Map static files
if (typeof options.files == "object") {
Object.keys(options.files).sort().reverse().forEach(function(key) {
console.log(key);
site.get("/" + key, function(req, res) {
console.log('Routing static file: ' + req.path);
return res.sendfile(options.files[key]);
});
});
}
// Add RESTful routes
var routes = require('./routes');
routes.route(site,options,Profile);
// Serve favicon.ico
site.use(express.favicon(options.favicon));
// Ensure all other routes go home for the client-side app
site.get("*", function(req, res) {
console.log('Routing to client: ' + req.path);
fs.createReadStream(options.index).pipe(res);
});
// Actually listen
site.listen(options.port, options.host);
});
exports.route = function(site,options,Profile) {
site.get('/api/profiles', function(req,res) {
Profile.find({}, function(err, docs) {
if (err) {console.log(err);}
res.json(docs);
});
});
site.get('/api/profiles/:id', function(req,res) {
Profile.find({_id: req.params.id}, function(err, doc) {
if (err) {console.log(err);}
res.json(doc);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment