├── node-http-server ├── bsconfig.json ├── lib │ └── js │ │ └── src │ │ ├── http_types.js │ │ ├── index.js │ │ └── test_http_server.js ├── README.md ├── package.json └── src │ ├── http_types.ml │ └── index.ml ├── README.md └── .gitignore /node-http-server/bsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "name" : "node-http-server", 3 | "sources" : "src" 4 | } -------------------------------------------------------------------------------- /node-http-server/lib/js/src/http_types.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.5.2, PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | 5 | 6 | /* No side effect */ 7 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | This repo provide users some examples showing how to use 3 | BuckleScript. 4 | 5 | For documentation of BuckleScript, see 6 | http://bucklescript.github.io/bucklescript/ 7 | 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *~ 2 | node_modules 3 | *.annot 4 | *.cmi 5 | *.cmj 6 | *.log 7 | *.d 8 | .bsbuild 9 | .bsdeps 10 | .ninja_deps 11 | .ninja_log 12 | *.mlast 13 | *.mliast 14 | .merlin 15 | bs/ 16 | ocaml/ 17 | 18 | build.ninja # we should not check in since it is OS dependent -------------------------------------------------------------------------------- /node-http-server/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | A small example to show how to make a http server using BuckleScript 4 | 5 | # [BuckleScript](http://bucklescript.github.io/bucklescript/) Demo 6 | 7 | 8 | 9 | 10 | ```sh 11 | npm install 12 | npm run build 13 | npm run server 14 | ``` 15 | 16 | -------------------------------------------------------------------------------- /node-http-server/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "devDependencies": { 3 | "bs-platform": "1.5.2" 4 | }, 5 | "scripts" : { 6 | "build" : "bsb -make-world", 7 | "server" : "node lib/js/src/index.js", 8 | "watch" : "bsb -make-world -w" 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /node-http-server/lib/js/src/index.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.5.2, PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Http = require("http"); 5 | var Pervasives = require("bs-platform/lib/js/pervasives"); 6 | 7 | var hostname = "127.0.0.1"; 8 | 9 | function create_server(http) { 10 | var server = http.createServer(function (_, resp) { 11 | resp.statusCode = 200; 12 | resp.setHeader("Content-Type", "text/plain"); 13 | return resp.end("Hello world\n"); 14 | }); 15 | return server.listen(3000, hostname, function () { 16 | console.log("Server running at http://" + (hostname + (":" + (Pervasives.string_of_int(3000) + "/")))); 17 | return /* () */0; 18 | }); 19 | } 20 | 21 | create_server(Http); 22 | 23 | /* Not a pure module */ 24 | -------------------------------------------------------------------------------- /node-http-server/lib/js/src/test_http_server.js: -------------------------------------------------------------------------------- 1 | // Generated by BUCKLESCRIPT VERSION 1.5.2, PLEASE EDIT WITH CARE 2 | 'use strict'; 3 | 4 | var Http = require("http"); 5 | var Pervasives = require("bs-platform/lib/js/pervasives"); 6 | 7 | var hostname = "127.0.0.1"; 8 | 9 | function create_server(http) { 10 | var server = http.createServer(function (_, resp) { 11 | resp.statusCode = 200; 12 | resp.setHeader("Content-Type", "text/plain"); 13 | return resp.end("Hello world\n"); 14 | }); 15 | return server.listen(3000, hostname, function () { 16 | console.log("Server running at http://" + (hostname + (":" + (Pervasives.string_of_int(3000) + "/")))); 17 | return /* () */0; 18 | }); 19 | } 20 | 21 | create_server(Http); 22 | 23 | /* Not a pure module */ 24 | -------------------------------------------------------------------------------- /node-http-server/src/http_types.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | (** A simple binding to node [http] module *) 26 | type req 27 | 28 | class type _resp = object 29 | method statusCode : int [@@bs.set] 30 | method setHeader : string -> string -> unit 31 | method _end : string -> unit 32 | end [@bs] 33 | 34 | type resp = _resp Js.t 35 | 36 | class type _server = object 37 | method listen : int -> string -> (unit -> unit [@bs]) -> unit 38 | end [@bs] 39 | type server = _server Js.t 40 | 41 | class type _http = object 42 | method createServer : (req -> resp -> unit [@bs] ) -> server 43 | end [@bs] 44 | 45 | type http = _http Js.t 46 | 47 | 48 | external http : http = "" [@@bs.module] 49 | -------------------------------------------------------------------------------- /node-http-server/src/index.ml: -------------------------------------------------------------------------------- 1 | (* Copyright (C) 2015-2016 Bloomberg Finance L.P. 2 | * 3 | * This program is free software: you can redistribute it and/or modify 4 | * it under the terms of the GNU Lesser General Public License as published by 5 | * the Free Software Foundation, either version 3 of the License, or 6 | * (at your option) any later version. 7 | * 8 | * In addition to the permissions granted to you by the LGPL, you may combine 9 | * or link a "work that uses the Library" with a publicly distributed version 10 | * of this file to produce a combined library or application, then distribute 11 | * that combined work under the terms of your choosing, with no requirement 12 | * to comply with the obligations normally placed on you by section 4 of the 13 | * LGPL version 3 (or the corresponding section of a later version of the LGPL 14 | * should you choose to use a later version). 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19 | * GNU Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public License 22 | * along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) 24 | 25 | [@@@bs.config {no_export}] 26 | let port = 3000 27 | let hostname = "127.0.0.1" 28 | let create_server http = 29 | let server = http##createServer begin fun [@bs] _req resp -> 30 | resp##statusCode #= 200; 31 | resp##setHeader "Content-Type" "text/plain"; 32 | resp##_end "Hello world\n" 33 | end 34 | in 35 | server##listen port hostname begin fun [@bs] () -> 36 | Js.log ("Server running at http://"^ hostname ^ ":" ^ Pervasives.string_of_int port ^ "/") 37 | end 38 | 39 | 40 | let () = 41 | create_server Http_types.http 42 | 43 | 44 | 45 | (* local variables: *) 46 | (* compile-command: "npm run --no-color build" *) 47 | (* end: *) 48 | --------------------------------------------------------------------------------