Skip to content

Instantly share code, notes, and snippets.

@MadcapJake
Last active August 29, 2015 14:14
Show Gist options
  • Save MadcapJake/21bd83dad31ebd76f29f to your computer and use it in GitHub Desktop.
Save MadcapJake/21bd83dad31ebd76f29f to your computer and use it in GitHub Desktop.
Ceylon Express attempt
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);
}
dynamic Path {
}
dynamic Server {
shared formal dynamic address();
}
"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;
});
}
@MadcapJake
Copy link
Author

Is this the right type for that get function?

shared formal void get(String path, 
        Anything(Request, Response)|Null|Anything(Request, Response)[] callbacks=null);

@MadcapJake
Copy link
Author

I added node_modules and am still getting the same error. However, I am not sure what you mean by run-js command. I'm using the IDE and the only place I can find to add repos is in the project properties menu under Ceylon Configuration > Module Repositories. Should I be added it to the Environment tab in Run Configurations?

@chochos
Copy link

chochos commented Jan 31, 2015

I think the problem is with return express(), not with require('express'). node.js throws when it can't find a module, and the message is Cannot find module 'blah'; your message is different. There's no express function defined anywhere in your code. If the express module contains it, you'd need to call it as requireExpress().express().

I also see a createApplication defined inside the Express dynamic interface. I haven't used express so I don't know if createApplication is really a function inside that module, but I see you define your own toplevel method with the same name.

@MadcapJake
Copy link
Author

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.

@MadcapJake
Copy link
Author

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