72 lines
1.6 KiB
Markdown
72 lines
1.6 KiB
Markdown
|
当 `app.js` 启动后,API 的测试地址为:
|
|||
|
**`http://localhost:3000/run`**
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
### 测试方法
|
|||
|
|
|||
|
#### 使用 `curl` 测试
|
|||
|
|
|||
|
1. **测试 C 代码**:
|
|||
|
```bash
|
|||
|
curl -X POST http://localhost:3000/run \
|
|||
|
-H "Content-Type: application/json" \
|
|||
|
-d '{
|
|||
|
"code": "#include <stdio.h>\nint main() { printf(\"Hello, World!\\n\"); return 0; }",
|
|||
|
"language": "c"
|
|||
|
}'
|
|||
|
```
|
|||
|
|
|||
|
2. **测试 Java 代码**:
|
|||
|
```bash
|
|||
|
curl -X POST http://localhost:3000/run \
|
|||
|
-H "Content-Type: application/json" \
|
|||
|
-d '{
|
|||
|
"code": "public class Main { public static void main(String[] args) { System.out.println(\"Hello, World!\"); } }",
|
|||
|
"language": "java"
|
|||
|
}'
|
|||
|
```
|
|||
|
|
|||
|
#### 使用 Postman 测试
|
|||
|
|
|||
|
1. 打开 [Postman](https://www.postman.com/)。
|
|||
|
2. 创建一个新的请求,选择 `POST` 方法。
|
|||
|
3. 设置请求 URL 为 `http://localhost:3000/run`。
|
|||
|
4. 在 `Headers` 中添加:
|
|||
|
```
|
|||
|
Key: Content-Type
|
|||
|
Value: application/json
|
|||
|
```
|
|||
|
5. 在 `Body` 中选择 `raw`,并输入测试数据,例如:
|
|||
|
```json
|
|||
|
{
|
|||
|
"code": "#include <stdio.h>\nint main() { printf(\"Hello, World!\\n\"); return 0; }",
|
|||
|
"language": "c"
|
|||
|
}
|
|||
|
```
|
|||
|
6. 点击发送,查看返回结果。
|
|||
|
|
|||
|
---
|
|||
|
|
|||
|
### 返回结果示例
|
|||
|
|
|||
|
1. **成功输出**:
|
|||
|
```json
|
|||
|
{
|
|||
|
"output": "Hello, World!\n"
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
2. **编译错误**(例如 C 代码语法错误):
|
|||
|
```json
|
|||
|
{
|
|||
|
"error": "main.c: In function 'main':\nmain.c:2:12: error: expected ';' before 'return'\n"
|
|||
|
}
|
|||
|
```
|
|||
|
|
|||
|
3. **运行时错误**(例如除零错误):
|
|||
|
```json
|
|||
|
{
|
|||
|
"error": "Floating point exception (core dumped)"
|
|||
|
}
|
|||
|
```
|