使用寓言中的节点模块

Using node modules from fable

我正在尝试在我的寓言代码中导入一个节点模块。作为寓言的新手,我确实预料到会出现问题,而理解导入流程似乎就是其中之一。我有下面的代码,它编译得很好,但在 printfn 语句

的行上 Cannot read property 'request' of undefined 失败了 运行 次
module Session =
    let inline f (f: 'a->'b->'c->'d) = Func<_,_,_,_> f
    [<Import("default","request")>]
    type Http = 
    abstract request : string -> System.Func<obj,obj,obj,unit> -> unit
    let http : Http = failwith "js only"
    let start () = 

       http.request "http://dr.dk" (ff (fun error response body ->
           printfn "%A" body 
       ))
    do  
        start()

我能够让您的示例在 Fable REPL 中运行:

open System
open Fable
open Fable.Core
open Fable.Core.JS
open Fable.Core.JsInterop

type RequestCallback = Func<obj, obj, obj, unit>

type Request = Func<string, RequestCallback, unit>

[<ImportDefault("request")>]
let request : Request = jsNative

let start () = 
    request.Invoke("http://dr.dk", (fun error response body ->
        console.log("error", error)
        console.log("response", response)
        console.log("body", body)
    ))

start ()

这是它生成的 JavaScript:

import request from "request";
import { some } from "fable-library/Option.js";

export function start() {
    request("http://dr.dk", (error, response, body) => {
        console.log(some("error"), error);
        console.log(some("response"), response);
        console.log(some("body"), body);
    });
}

start();

请注意,已经有许多模块的绑定。对于这个特定任务,我建议使用 Fable.Fetch. If you want a library that works in the browser and .NET, try Fable.SimpleHttp.