-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (35 loc) · 1.32 KB
/
Copy pathserver.js
File metadata and controls
37 lines (35 loc) · 1.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const express = require("express");
const pty = require("node-pty");
const app = express();
const expressWs = require("express-ws")(app);
const hostname = '127.0.0.1';
const port = 8800;
app.use(express.static(__dirname));
// shell(terminal)のインスタンス化とdataの扱い
expressWs.app.ws("/shell", function (ws, req) {
// Spawn the shell
// Compliments of http://krasimirtsonev.com/blog/article/meet-evala-your-terminal-in-the-browser-extension
let shell = pty.spawn("/bin/bash", [], {
name: "xterm-color",
// このプロジェクトのpathになっている
cwd: process.env.PWD,
// 環境設定(これのおかげでterminalの設定が自分で設定した仕様になっている)
env: process.env
});
// shell(terminal)から帰ってきた値をwebに表示させる
// websocketを使っている理由は常時通信させるため
// shell(Terminal)にeventを設置して、何か変化があったらそのデータをwebに送信している
// 出力専門
shell.on("data", function (data) {
ws.send(data);
});
// 入力された値をshell(terminal)に送る
// terminalに入力
ws.on("message", function (msg) {
shell.write(msg);
});
});
// serverの立ち上げ
app.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});