Skip to content

Instantly share code, notes, and snippets.

@chikatoike
Created March 31, 2013 07:57
Show Gist options
  • Save chikatoike/5279926 to your computer and use it in GitHub Desktop.
Save chikatoike/5279926 to your computer and use it in GitHub Desktop.
Using express with FunScript.
[<ReflectedDefinition>]
module Program
open FunScript
open FunScript.TypeScript
open FunScript.Compiler
// Imports
// Note: this path is relative to the directory you launched mono / monodevelop.
type ts = Api< @"../Typings/node-0.8.d.ts" >
type expressModule = Api< @"../Typings/express-3.0.d.ts" >
let expressApp() =
let express = ts.require.Invoke "express"
let app = expressModule.express.Invoke()
app.get("/", unbox<expressModule._express.Handler>(new System.Action<expressModule._express.ServerRequest, expressModule._express.ServerResponse>(fun req res ->
res.send("Hello world!") |> ignore
()
))) |> ignore
app.listen(8124)
()
// // Compile to JS:
let Run(expression, components, directory) =
let thisAsm = System.Reflection.Assembly.GetExecutingAssembly()
let root = System.IO.Path.GetDirectoryName(thisAsm.Location)
let root = System.IO.Path.Combine(root, directory)
// Compile the main function into a script
let sw = System.Diagnostics.Stopwatch.StartNew()
let source = FunScript.Compiler.Compiler.Compile(expression, components=components, noReturn=true)
let sourceWrapped = sprintf "(function() {\n%s\n})();" source
let filename = System.IO.Path.Combine(root, (System.IO.Path.GetFileNameWithoutExtension(thisAsm.Location).ToLower()) + ".js")
printfn "Generated JavaScript in %f sec..." (float sw.ElapsedMilliseconds / 1000.0)
System.IO.File.Delete filename
System.IO.File.WriteAllText(filename, sourceWrapped)
Run(<@ expressApp() @>, Interop.Components.all, "..\\..")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment