Skip to content

Instantly share code, notes, and snippets.

@ag4ve
Created September 11, 2012 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ag4ve/3697982 to your computer and use it in GitHub Desktop.
Save ag4ve/3697982 to your computer and use it in GitHub Desktop.
flatiron app
var path = require('path'),
flatiron = require('flatiron'),
director = require('director'),
ecstatic = require('ecstatic'),
connect = require('connect'),
app = flatiron.app,
plugins = flatiron.plugins;
var routes = require('./lib/routes.js'),
models = require('./lib/models.js');
global.app = app;
global.models = models;
app.config.file({ file: path.join(__dirname, 'config', 'config.json') });
app.use(plugins.http);
app.http.before = [
connect.favicon(),
connect.logger(),
ecstatic(__dirname + '/public', opts = {
cache: 600,
autoIndex: false
}),
connect.cookieParser(),
connect.bodyParser(),
];
// start db from routes
models.init();
// mount routes
app.router.mount(routes);
app.start(3000);
var addr = app.server.address();
app.log.info('Listening on http://' + addr.address + ':' + addr.port);
var routes = {
'/fr': {
'get': function () {
var that = this;
models.fr.get({}, null, function (err, data) {
if (err) {
app.log.warn(err);
that.res.end("fail");
} else {
that.res.end(JSON.stringify(data));
}
} );
},
'post': function() {
var that = this;
console.log("json stringify: " + JSON.stringify(that.req.body));
console.log("trying something" + JSON.parse(that.req.body));
var input = JSON.parse(that.req.body);
console.log("json parse body: " + input);
models.fr.put(input, function (err, input) {
if (err) {
app.log.warn(err);
that.res.end("fail");
} else {
that.res.end("ok");
}
} );
},
},
'/test': {
"get": function () {
this.res.end(JSON.stringify({ "hello": "world" }));
},
},
};
module.exports = routes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment