Skip to content

Instantly share code, notes, and snippets.

@aaronpowell
Last active August 29, 2015 14:01
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 aaronpowell/b7ab563ea8dac06a7d8c to your computer and use it in GitHub Desktop.
Save aaronpowell/b7ab563ea8dac06a7d8c to your computer and use it in GitHub Desktop.
Express.js + Sweet.js + modules
var express = require('express');
var app = express();
get '/404' send(404) 'Not Found'
get '/foo' with (req, res) {
res.json({ foo: 'bar' });
}
get '/' send 'Hello World'
app.listen(3000);
macro copy_context {
case { _ ($from, $to) } => {
return [makeIdent(unwrapSyntax(#{ $to }), #{ $from })];
}
}
macroclass status {
pattern { $method:ident($code:lit) }
}
let get = macro {
case { $m $path:lit $do $res:lit } => {
return #{
copy_context($m, app).get($path, function (req, res) {
res.$do($res);
});
}
}
case { $m $path:lit $what:status $res:lit } => {
return #{
copy_context($m, app).get($path, function (req, res) {
res.$what$method($what$code, $res);
});
}
}
case { $m $path:lit with ($req:ident, $res:ident) { $body ... } } => {
return #{
copy_context($m, app).get($path, function ($req, $res) {
$body ...
});
}
}
}
export get
{
"name": "sweetjs-express",
"version": "0.0.0",
"description": "",
"main": "app.js",
"dependencies": {
"express": "^4.2.0",
"sweet.js": "^0.6.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Aaron Powell",
"license": "ISC"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment