2023-07-29 15:27:03 +03:00
|
|
|
# JS DOM Cube
|
2022-08-03 13:34:26 +03:00
|
|
|
|
2023-07-29 15:27:03 +03:00
|
|
|
## Compiling
|
2022-08-03 13:34:26 +03:00
|
|
|
|
2023-07-29 15:27:03 +03:00
|
|
|
```
|
|
|
|
v -b js_browser examples/js_dom_cube/cube.js.v
|
|
|
|
```
|
|
|
|
|
|
|
|
Then you can open `index.html` in your favorite browser.
|
|
|
|
|
|
|
|
## Serve Examples
|
2022-08-03 13:34:26 +03:00
|
|
|
|
2023-07-29 15:27:03 +03:00
|
|
|
### JS Server
|
|
|
|
|
|
|
|
> **NOTE**\
|
|
|
|
> The JS server example in the following steps requires Node.js.
|
|
|
|
> To install Node, please refer to the [download page](https://nodejs.org/en/download/)
|
|
|
|
> or the installation via your operating systems [package manager](https://nodejs.org/en/download/package-manager).
|
|
|
|
|
|
|
|
Initialize the example as a Node project
|
2021-11-24 21:31:39 +03:00
|
|
|
|
|
|
|
```
|
2023-07-29 15:27:03 +03:00
|
|
|
cd examples/js_dom_cube/
|
|
|
|
npm init -y
|
2021-11-24 21:31:39 +03:00
|
|
|
```
|
2022-08-03 13:34:26 +03:00
|
|
|
|
2023-07-29 15:27:03 +03:00
|
|
|
Add a `start` and `build` script to the generated `./package.json` file.
|
2022-08-03 13:34:26 +03:00
|
|
|
|
|
|
|
```json
|
|
|
|
"scripts": {
|
2023-07-29 15:27:03 +03:00
|
|
|
...
|
2022-08-03 13:34:26 +03:00
|
|
|
"start": "npm run build && node server.js",
|
2023-07-29 15:27:03 +03:00
|
|
|
"build": "v -b js_browser cube.js.v"
|
2022-08-03 13:34:26 +03:00
|
|
|
},
|
|
|
|
```
|
2023-07-29 15:27:03 +03:00
|
|
|
|
|
|
|
Below is an example of a Node.js server without external dependencies.
|
|
|
|
You can use it for `./server.js`.
|
2022-08-03 13:34:26 +03:00
|
|
|
|
|
|
|
```javascript
|
2023-07-29 15:27:03 +03:00
|
|
|
const http = require('http');
|
|
|
|
const fs = require('fs');
|
2022-08-03 13:34:26 +03:00
|
|
|
var path = require('path');
|
|
|
|
|
2023-07-29 15:27:03 +03:00
|
|
|
const host = 'localhost';
|
2022-08-03 13:34:26 +03:00
|
|
|
const port = 3000;
|
|
|
|
|
|
|
|
const reqListener = function (req, res) {
|
2023-07-29 15:27:03 +03:00
|
|
|
console.log('[route] - ', req.url);
|
|
|
|
|
|
|
|
var filePath = '.' + req.url;
|
|
|
|
if (filePath == './') {
|
|
|
|
filePath = './index.html';
|
|
|
|
}
|
|
|
|
|
|
|
|
var extname = String(path.extname(filePath)).toLowerCase();
|
|
|
|
var mimeTypes = {
|
|
|
|
'.html': 'text/html',
|
|
|
|
'.js': 'text/javascript',
|
|
|
|
'.css': 'text/css',
|
|
|
|
'.json': 'application/json',
|
|
|
|
'.png': 'image/png',
|
|
|
|
'.jpg': 'image/jpg',
|
|
|
|
'.gif': 'image/gif',
|
|
|
|
'.svg': 'image/svg+xml',
|
|
|
|
'.wav': 'audio/wav',
|
|
|
|
'.mp4': 'video/mp4',
|
|
|
|
'.woff': 'application/font-woff',
|
|
|
|
'.ttf': 'application/font-ttf',
|
|
|
|
'.eot': 'application/vnd.ms-fontobject',
|
|
|
|
'.otf': 'application/font-otf',
|
|
|
|
'.wasm': 'application/wasm',
|
|
|
|
};
|
|
|
|
|
|
|
|
var contentType = mimeTypes[extname] || 'application/octet-stream';
|
|
|
|
|
|
|
|
fs.readFile(filePath, function (error, content) {
|
|
|
|
if (error) {
|
|
|
|
if (error.code == 'ENOENT') {
|
|
|
|
fs.readFile('./404.html', function (error, content) {
|
|
|
|
res.writeHead(404, { 'Content-Type': 'text/html' });
|
|
|
|
res.end(content, 'utf-8');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
res.writeHead(500);
|
|
|
|
res.end('Sorry, check with the site admin for error: ' + error.code + ' ..\n');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
res.writeHead(200, { 'Content-Type': contentType });
|
|
|
|
res.end(content, 'utf-8');
|
|
|
|
}
|
|
|
|
});
|
2022-08-03 13:34:26 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const server = http.createServer(reqListener);
|
|
|
|
server.listen(port, host, () => {
|
2023-07-29 15:27:03 +03:00
|
|
|
console.log(`Server is running on http://${host}:${port}`);
|
2022-08-03 13:34:26 +03:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2023-07-29 15:27:03 +03:00
|
|
|
Now you can build and run the project with the added scripts.
|
2022-08-03 13:34:26 +03:00
|
|
|
|
2023-07-29 15:27:03 +03:00
|
|
|
```sh
|
|
|
|
npm run build
|
|
|
|
npm run start
|
2022-08-03 13:34:26 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
### V server
|
2023-07-29 15:27:03 +03:00
|
|
|
|
|
|
|
The example below uses `vweb` to serve the project.
|
|
|
|
|
|
|
|
```v
|
2022-08-03 13:34:26 +03:00
|
|
|
module main
|
|
|
|
|
|
|
|
import vweb
|
|
|
|
import os
|
|
|
|
|
|
|
|
const (
|
2023-07-29 15:27:03 +03:00
|
|
|
http_port = 3001
|
2022-08-03 13:34:26 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
struct App {
|
2023-07-29 15:27:03 +03:00
|
|
|
vweb.Context
|
2022-08-03 13:34:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
2023-07-29 15:27:03 +03:00
|
|
|
vweb.run(new_app(), http_port)
|
2022-08-03 13:34:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (mut app App) before_request() {
|
2023-07-29 15:27:03 +03:00
|
|
|
// Build the cube.js javascript file
|
|
|
|
os.execute_or_panic('v -b js_browser cube.js.v ')
|
2022-08-03 13:34:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_app() &App {
|
2023-07-29 15:27:03 +03:00
|
|
|
mut app := &App{}
|
|
|
|
app.serve_static('/favicon.ico', 'favicon.ico')
|
|
|
|
app.serve_static('/cube.js', 'cube.js')
|
|
|
|
app.mount_static_folder_at(os.resource_abs_path('.'), '/')
|
|
|
|
return app
|
2022-08-03 13:34:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
['/'; get]
|
|
|
|
pub fn (mut app App) controller_get_all_task() vweb.Result {
|
2023-07-29 15:27:03 +03:00
|
|
|
file := os.read_file('./index.html') or { panic(err) }
|
|
|
|
return app.html(file)
|
2022-08-03 13:34:26 +03:00
|
|
|
}
|
2023-07-29 15:27:03 +03:00
|
|
|
```
|