iT邦幫忙

2019 iT 邦幫忙鐵人賽

DAY 17
0
自我挑戰組

Some thing with Reason系列 第 17

Reason 搭配 NPM 使用 bs-express 套件

建立 Semver 的類別

semver 在 Javascript 可以使用 new 關鍵字來產生物件

const s = new semver("1.5.0");
s.minor(); // 5

在 Reason 中可以使用 class type 來綁定


class type semverInstance =
[@bs]
{
  pub version: string;
  pub major: int;
  pub minor: int;
  pub patch: int;
  pub raw: string;
  pub build: array(string);
  pub prerelease: array(string)
};

type tSemver = Js.t(semverInstance);

完成了這一步先回想一下如何發布 NPM 的套件

發布 NPM 套件

document

將寫完的 Reason 檔案透過上面的步驟就可以推到 NPM 上面

使用 NPM 上的 bs-express 套件

  $ npm install bs-express express

bsconfig.json

"bs-dependencies": [
    "bs-express"
  ],

Demo.re

open Express;

let app = express();

let onListen = e =>
  switch (e) {
  | exception (Js.Exn.Error(e)) =>
    Js.log(e);
    Node.Process.exit(1);
  | _ => Js.log @@ "Listening at http://127.0.0.1:3000"
  };

  App.get(app, ~path="/") @@
Middleware.from((next, req,) => {
  Response.sendString("Hello world")
});

let server = App.listen(app, ~port=3000, ~onListen, ());
  $ node src/Demo.bs.js

Demo Url

你就可以看到 Hello World!

終於串起 Reason 和 NPM 的部分

接下來是怎麼處理 使用 Reason 讓 Javascript 使用呢?


上一篇
External Promise API
下一篇
Create NPM Package Part-I
系列文
Some thing with Reason30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言