-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.json.ts
51 lines (39 loc) · 1018 Bytes
/
index.json.ts
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
38
39
40
41
42
43
44
45
46
47
import type { Request } from "@sveltejs/kit"
import type { RequestHandler } from "@sveltejs/kit"
import { spawn } from "child_process"
let output = ``
function makeSystemCall() {
const systemCall = spawn(`ls`, [`-al`])
systemCall.stdout.on(`data`, (data) => {
output += data
console.log(`stdout: ${data}`)
})
systemCall.stderr.on(`data`, (data) => {
console.error(`stderr: ${data}`)
})
systemCall.on(`close`, (code) => {
console.log(`child process exited with code ${code}`)
})
}
// you get a 404 without this request handler
export const get: RequestHandler = (request) => {
return api(request)
}
// api
export const api = (request: Request) => {
let body = {}
let status = 500
switch (request.method.toUpperCase()) {
case "GET":
makeSystemCall()
body = output
status = 200
break
default:
break
}
return {
status,
body,
}
}