Last active
August 29, 2015 14:14
-
-
Save MadcapJake/21bd83dad31ebd76f29f to your computer and use it in GitHub Desktop.
Ceylon Express attempt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dynamic Express {} | |
Application requireExpressAndCallConstructor() { | |
dynamic { return require("express")(); } | |
} | |
dynamic Request {} | |
dynamic Response { | |
shared formal void send(String string); | |
} | |
dynamic Application { | |
shared formal void get(String path, | |
Anything(Request, Response)|Null|Anything(Request, Response)[] callbacks=null); | |
shared formal Server listen(Integer integer, Anything() callable); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dynamic Path { | |
} | |
dynamic Server { | |
shared formal dynamic address(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"Run the module `my.express`." | |
shared void run() { | |
Application app = requireExpressAndCallConstructor(); | |
app.get("/", (Request req, Response res) { | |
return res.send("Hello world!"); | |
}); | |
Server server = app.listen(3000, () { | |
//dynamic host = server.address().address; | |
//dynamic port = server.address().port; | |
dynamic { | |
console.log( | |
"Example app listening..."// at http://%s:%s", | |
//host, | |
//port | |
); | |
} | |
return nothing; | |
}); | |
} |
I've updated the code per your suggestions @chochos and I am now getting a new error that I don't understand 😕
if(type.t.$$.T$name in obj.getT$all()){
^
TypeError: Cannot use 'in' operator to search for 'my.express::Application' in undefined
at is$ (C:\Users\madca_000\.eclipse\org.eclipse.platform_4.4.1_1167611518_win32_win32_x86_64\plugins\com.redhat.ceylon.dist.repo_1.1.0.v20141013-1416\repo\ceylon\language\1.1.0\ceylon.language-1.1.0.js:188:28)
at Object.dre$$ (C:\Users\madca_000\.eclipse\org.eclipse.platform_4.4.1_1167611518_win32_win32_x86_64\plugins\com.redhat.ceylon.dist.repo_1.1.0.v20141013-1416\repo\ceylon\language\1.1.0\ceylon.language-1.1.0.js:634:5)
at requireExpressAndCallConstructor (C:\Users\madca_000\eclipse\ceylon-express-raw\modules\my\express\1.0.0\my.express-1.0.0.js:76:17)
at run (C:\Users\madca_000\eclipse\ceylon-express-raw\modules\my\express\1.0.0\my.express-1.0.0.js:13:13)
at [eval]:1:292
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (module.js:456:26)
at evalScript (node.js:536:25)
at startup (node.js:80:7)
at node.js:906:3
I know it has to do with calling the return of require
(i.e., require("express")();
) but that is how the JavaScript is done. Here is the example given in the Express.js docs:
var express = require('express')
var app = express()
app.get('/', function (req, res) {
...
})
var server = app.listen(3000, function () {
...
})
And when I don't call the return (i.e., require("express");
), I get this:
$3i.$_get("/",m$3h.$JsCallable((function($3j,$3k){
^
TypeError: Object function createApplication() {
var app = function(req, res, next) {
app.handle(req, res, next);
};
mixin(app, proto);
mixin(app, EventEmitter.prototype);
app.request = { __proto__: req, app: app };
app.response = { __proto__: res, app: app };
app.init();
return app;
} has no method '$_get'
at run (C:\Users\madca_000\eclipse\ceylon-express-raw\modules\my\express\1.0.0\my.express-1.0.0.js:14:9)
at [eval]:1:292
at Object.<anonymous> ([eval]-wrapper:6:22)
at Module._compile (module.js:456:26)
at evalScript (node.js:536:25)
at startup (node.js:80:7)
at node.js:906:3
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yeah I was trying to get around the fact that in JavaScript you call the express constructor with 'express()' and I couldn't figure out how to get that to work in Ceylon because it seems like the compiler is looking for some function that I'm referencing there. I'm away from my computer but will
requireExpress.express()
run Express' constructor?The
createApplication
function was what I thought Express was calling when it constructed but Express gave me an error when I tried to call it so I was just using the name to encapsulate the dynamic return of the constructor and also as a place to add the Application type to what the constructor returns.