通过 curl 使用 REPL
🌐 REPL over curl
这是一个关于如何在 curl() 上运行 REPL 实例的示例
🌐 This is an example on how to run a REPL instance over curl()
以下脚本在端口 8000 上启动一个 HTTP 服务器,能够接受通过 curl() 建立的连接。
🌐 The following script starts an HTTP server on port 8000 that can accept
a connection established via curl().
import http from 'node:http'; import repl from 'node:repl'; const server = http.createServer((req, res) => { res.setHeader('content-type', 'multipart/octet-stream'); repl.start({ prompt: 'curl repl> ', input: req, output: res, terminal: false, useColors: true, useGlobal: false, }); }); server.listen(8000);const http = require('node:http'); const repl = require('node:repl'); const server = http.createServer((req, res) => { res.setHeader('content-type', 'multipart/octet-stream'); repl.start({ prompt: 'curl repl> ', input: req, output: res, terminal: false, useColors: true, useGlobal: false, }); }); server.listen(8000);
当上述脚本正在运行时,你可以使用 curl() 连接到服务器,并通过运行 curl --no-progress-meter -sSNT. localhost:8000 连接到它的 REPL 实例。
🌐 When the above script is running you can then use curl() to connect to
the server and connect to its REPL instance by running curl --no-progress-meter -sSNT. localhost:8000.
警告 这个示例仅用于教育目的,演示如何使用不同的 I/O 流启动 Node.js REPL。
在生产环境或任何涉及安全性的场景中,不应 使用此示例,除非采取了额外的防护措施。
如果需要在实际应用中实现 REPL,请考虑采用能够降低这些风险的替代方法,例如使用安全的输入机制并避免开放的网络接口。