|
1 | | -# Serve |
| 1 | +# JS DOM Cube |
2 | 2 |
|
3 | | -This project has a no dependence serve in `./server.js` path. |
4 | | -That can be run with `node` command after build. |
| 3 | +## Compiling |
5 | 4 |
|
6 | | -or just run: `npm run start` |
| 5 | +``` |
| 6 | +v -b js_browser examples/js_dom_cube/cube.js.v |
| 7 | +``` |
| 8 | + |
| 9 | +Then you can open `index.html` in your favorite browser. |
| 10 | + |
| 11 | +## Serve Examples |
7 | 12 |
|
8 | | -To install node, you can access node [download page](https://nodejs.org/en/download/) |
9 | | -or [package manager](https://nodejs.org/en/download/package-manager) |
10 | | -Drawing with mouse events using DOM API. Adopted from MDN examples. |
| 13 | +### JS Server |
| 14 | + |
| 15 | +> **NOTE**\ |
| 16 | +> The JS server example in the following steps requires Node.js. |
| 17 | +> To install Node, please refer to the [download page](https://nodejs.org/en/download/) |
| 18 | +> or the installation via your operating systems [package manager](https://nodejs.org/en/download/package-manager). |
| 19 | +
|
| 20 | +Initialize the example as a Node project |
11 | 21 |
|
12 | | -# Compiling |
13 | 22 | ``` |
14 | | -v -b js_browser examples/js_dom_cube/cube.js.v |
| 23 | +cd examples/js_dom_cube/ |
| 24 | +npm init -y |
15 | 25 | ``` |
16 | 26 |
|
17 | | -Drawing with mouse events using DOM API. Adopted from MDN examples. |
18 | | -Then you can open `index.html` with your favourite browser. |
19 | | -# Serve examples |
| 27 | +Add a `start` and `build` script to the generated `./package.json` file. |
20 | 28 |
|
21 | | -### JS server |
22 | | -After run `npm init -y` code and genared `./package.json` |
23 | | -You can put `start` and `build` at script in jason leaf. |
24 | | -`path './package.json'` |
25 | 29 | ```json |
26 | 30 | "scripts": { |
| 31 | + ... |
27 | 32 | "start": "npm run build && node server.js", |
28 | | - "build":"v -b js_browser cube.js.v" |
| 33 | + "build": "v -b js_browser cube.js.v" |
29 | 34 | }, |
30 | | - |
31 | 35 | ``` |
32 | | -here is the pure javascript server code |
33 | | -`path './server.js'` |
| 36 | + |
| 37 | +Below is an example of a Node.js server without external dependencies. |
| 38 | +You can use it for `./server.js`. |
34 | 39 |
|
35 | 40 | ```javascript |
36 | | -const http = require("http"); |
37 | | -const fs = require("fs"); |
| 41 | +const http = require('http'); |
| 42 | +const fs = require('fs'); |
38 | 43 | var path = require('path'); |
39 | 44 |
|
40 | | -const host = "localhost"; |
| 45 | +const host = 'localhost'; |
41 | 46 | const port = 3000; |
42 | 47 |
|
43 | 48 | const reqListener = function (req, res) { |
44 | | - console.log('[route] - ', req.url); |
45 | | - |
46 | | - var filePath = '.' + req.url; |
47 | | - if (filePath == './') { |
48 | | - filePath = './index.html'; |
49 | | - } |
50 | | - |
51 | | - var extname = String(path.extname(filePath)).toLowerCase(); |
52 | | - var mimeTypes = { |
53 | | - '.html': 'text/html', |
54 | | - '.js': 'text/javascript', |
55 | | - '.css': 'text/css', |
56 | | - '.json': 'application/json', |
57 | | - '.png': 'image/png', |
58 | | - '.jpg': 'image/jpg', |
59 | | - '.gif': 'image/gif', |
60 | | - '.svg': 'image/svg+xml', |
61 | | - '.wav': 'audio/wav', |
62 | | - '.mp4': 'video/mp4', |
63 | | - '.woff': 'application/font-woff', |
64 | | - '.ttf': 'application/font-ttf', |
65 | | - '.eot': 'application/vnd.ms-fontobject', |
66 | | - '.otf': 'application/font-otf', |
67 | | - '.wasm': 'application/wasm' |
68 | | - }; |
69 | | - |
70 | | - var contentType = mimeTypes[extname] || 'application/octet-stream'; |
71 | | - |
72 | | - fs.readFile(filePath, function(error, content) { |
73 | | - if (error) { |
74 | | - if(error.code == 'ENOENT') { |
75 | | - fs.readFile('./404.html', function(error, content) { |
76 | | - res.writeHead(404, { 'Content-Type': 'text/html' }); |
77 | | - res.end(content, 'utf-8'); |
78 | | - }); |
79 | | - } |
80 | | - else { |
81 | | - res.writeHead(500); |
82 | | - res.end('Sorry, check with the site admin for error: '+error.code+' ..\n'); |
83 | | - } |
84 | | - } |
85 | | - else { |
86 | | - res.writeHead(200, { 'Content-Type': contentType }); |
87 | | - res.end(content, 'utf-8'); |
88 | | - } |
89 | | - }); |
| 49 | + console.log('[route] - ', req.url); |
| 50 | + |
| 51 | + var filePath = '.' + req.url; |
| 52 | + if (filePath == './') { |
| 53 | + filePath = './index.html'; |
| 54 | + } |
| 55 | + |
| 56 | + var extname = String(path.extname(filePath)).toLowerCase(); |
| 57 | + var mimeTypes = { |
| 58 | + '.html': 'text/html', |
| 59 | + '.js': 'text/javascript', |
| 60 | + '.css': 'text/css', |
| 61 | + '.json': 'application/json', |
| 62 | + '.png': 'image/png', |
| 63 | + '.jpg': 'image/jpg', |
| 64 | + '.gif': 'image/gif', |
| 65 | + '.svg': 'image/svg+xml', |
| 66 | + '.wav': 'audio/wav', |
| 67 | + '.mp4': 'video/mp4', |
| 68 | + '.woff': 'application/font-woff', |
| 69 | + '.ttf': 'application/font-ttf', |
| 70 | + '.eot': 'application/vnd.ms-fontobject', |
| 71 | + '.otf': 'application/font-otf', |
| 72 | + '.wasm': 'application/wasm', |
| 73 | + }; |
| 74 | + |
| 75 | + var contentType = mimeTypes[extname] || 'application/octet-stream'; |
| 76 | + |
| 77 | + fs.readFile(filePath, function (error, content) { |
| 78 | + if (error) { |
| 79 | + if (error.code == 'ENOENT') { |
| 80 | + fs.readFile('./404.html', function (error, content) { |
| 81 | + res.writeHead(404, { 'Content-Type': 'text/html' }); |
| 82 | + res.end(content, 'utf-8'); |
| 83 | + }); |
| 84 | + } else { |
| 85 | + res.writeHead(500); |
| 86 | + res.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n'); |
| 87 | + } |
| 88 | + } else { |
| 89 | + res.writeHead(200, { 'Content-Type': contentType }); |
| 90 | + res.end(content, 'utf-8'); |
| 91 | + } |
| 92 | + }); |
90 | 93 | }; |
91 | 94 |
|
92 | 95 | const server = http.createServer(reqListener); |
93 | 96 | server.listen(port, host, () => { |
94 | | - console.log(`Server is running on http://${host}:${port}`); |
| 97 | + console.log(`Server is running on http://${host}:${port}`); |
95 | 98 | }); |
96 | | - |
97 | 99 | ``` |
98 | 100 |
|
99 | | -This project has a no dependence serve in `./server.js` path. |
100 | | -That can be run with `node` command after build. |
101 | | - |
102 | | -or just run: `npm run start` |
| 101 | +Now you can build and run the project with the added scripts. |
103 | 102 |
|
104 | | -To install node, you can access node [download page](https://nodejs.org/en/download/) |
105 | | -or [package manager](https://nodejs.org/en/download/package-manager) |
106 | | - |
107 | | -``` |
108 | | -$ cd examples/js_dom_draw/ |
109 | | -$ npm run build |
| 103 | +```sh |
| 104 | +npm run build |
| 105 | +npm run start |
110 | 106 | ``` |
111 | 107 |
|
112 | | - |
113 | 108 | ### V server |
114 | | -```v ignore |
| 109 | + |
| 110 | +The example below uses `vweb` to serve the project. |
| 111 | + |
| 112 | +```v |
115 | 113 | module main |
116 | 114 |
|
117 | 115 | import vweb |
118 | 116 | import os |
119 | 117 |
|
120 | 118 | const ( |
121 | | - http_port = 3001 |
| 119 | + http_port = 3001 |
122 | 120 | ) |
123 | 121 |
|
124 | 122 | struct App { |
125 | | - vweb.Context |
| 123 | + vweb.Context |
126 | 124 | } |
127 | 125 |
|
128 | 126 | fn main() { |
129 | | - vweb.run(new_app(), http_port) |
| 127 | + vweb.run(new_app(), http_port) |
130 | 128 | } |
131 | 129 |
|
132 | 130 | pub fn (mut app App) before_request() { |
133 | | - // This build server json files |
134 | | - os.execute_or_panic('v -b js_browser cube.js.v ') |
| 131 | + // Build the cube.js javascript file |
| 132 | + os.execute_or_panic('v -b js_browser cube.js.v ') |
135 | 133 | } |
136 | 134 |
|
137 | 135 | fn new_app() &App { |
138 | | - mut app := &App{} |
139 | | - app.serve_static('/favicon.ico', 'favicon.ico') |
140 | | - app.serve_static('/cube.js', 'cube.js') |
141 | | - app.mount_static_folder_at(os.resource_abs_path('.'), '/') |
142 | | - return app |
| 136 | + mut app := &App{} |
| 137 | + app.serve_static('/favicon.ico', 'favicon.ico') |
| 138 | + app.serve_static('/cube.js', 'cube.js') |
| 139 | + app.mount_static_folder_at(os.resource_abs_path('.'), '/') |
| 140 | + return app |
143 | 141 | } |
144 | 142 |
|
145 | 143 | ['/'; get] |
146 | 144 | pub fn (mut app App) controller_get_all_task() vweb.Result { |
147 | | - file :=os.read_file('./index.html') or { panic(err) } |
148 | | - return app.html(file) |
| 145 | + file := os.read_file('./index.html') or { panic(err) } |
| 146 | + return app.html(file) |
149 | 147 | } |
150 | | -``` |
| 148 | +``` |
0 commit comments