《Node.js 调用 Python:项目启动与配置指南》
2025-05-07 09:28:20作者:廉彬冶Miranda
1. 项目的目录结构及介绍
node-calls-python 项目是一个使用 Node.js 调用 Python 代码的示例项目。项目的目录结构如下所示:
node-calls-python/
├── bin/ # 存放可执行文件
│ └── www # 启动服务器脚本
├── lib/ # 存放项目的库文件
│ └── python_caller.js
├── node_modules/ # 存放项目依赖的 Node.js 模块
├── package.json # 项目配置文件
├── .env # 环境变量配置文件
└── app.js # 项目的入口文件
bin/: 存放项目启动脚本和其他可执行文件。lib/: 存放项目自定义的库文件,例如python_caller.js用于封装 Node.js 调用 Python 的逻辑。node_modules/: 通过npm install命令安装的项目依赖模块。package.json: 定义项目依赖、脚本和元数据。.env: 项目环境变量配置文件。app.js: 项目的入口文件,用于启动 Node.js 服务器。
2. 项目的启动文件介绍
项目的启动文件是 bin/www。该文件负责启动 Node.js 服务器,其核心代码如下:
#!/usr/bin/env node
/**
* Module dependencies.
*/
const app = require('../app');
const debug = require('debug')('node-calls-python:server');
const http = require('http');
/**
* Get port from environment and store in Express.
*/
const port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
const port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
const bind = typeof port === 'string' ? 'Pipe ' + port : 'Port ' + port;
// Handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
const addr = server.address();
const bind = typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port;
debug('Listening on ' + bind);
}
启动文件通过 node bin/www 命令运行。
3. 项目的配置文件介绍
项目的配置文件是 .env,这是一个简单的环境变量配置文件,用于定义项目运行时所需的环境变量。其内容可能如下:
PORT=3000
PYTHON_PATH=/usr/bin/python3
PORT: 定义了 Node.js 服务器的监听端口。PYTHON_PATH: 定义了 Python 解释器的路径,确保 Node.js 能够正确调用 Python。
在 .env 文件中定义环境变量后,可以在项目中的任何位置通过 process.env.VARIABLE_NAME 访问这些变量。例如,process.env.PORT 将返回 '3000'。
确保在启动项目之前,正确设置了这些环境变量。
登录后查看全文
热门项目推荐
atomcodeClaude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed. Get StartedRust0144- DDeepSeek-V4-ProDeepSeek-V4-Pro(总参数 1.6 万亿,激活 49B)面向复杂推理和高级编程任务,在代码竞赛、数学推理、Agent 工作流等场景表现优异,性能接近国际前沿闭源模型。Python00
GLM-5.1GLM-5.1是智谱迄今最智能的旗舰模型,也是目前全球最强的开源模型。GLM-5.1大大提高了代码能力,在完成长程任务方面提升尤为显著。和此前分钟级交互的模型不同,它能够在一次任务中独立、持续工作超过8小时,期间自主规划、执行、自我进化,最终交付完整的工程级成果。Jinja00
MiniCPM-V-4.6这是 MiniCPM-V 系列有史以来效率与性能平衡最佳的模型。它以仅 1.3B 的参数规模,实现了性能与效率的双重突破,在全球同尺寸模型中登顶,全面超越了阿里 Qwen3.5-0.8B 与谷歌 Gemma4-E2B-it。Jinja00
Intern-S2-PreviewIntern-S2-Preview,这是一款高效的350亿参数科学多模态基础模型。除了常规的参数与数据规模扩展外,Intern-S2-Preview探索了任务扩展:通过提升科学任务的难度、多样性与覆盖范围,进一步释放模型能力。Python00
skillhubopenJiuwen 生态的 Skill 托管与分发开源方案,支持自建与可选 ClawHub 兼容。Python0110
热门内容推荐
最新内容推荐
项目优选
收起
暂无描述
Dockerfile
730
4.71 K
Ascend Extension for PyTorch
Python
604
774
openEuler内核是openEuler操作系统的核心,既是系统性能与稳定性的基石,也是连接处理器、设备与服务的桥梁。
C
432
388
本项目是CANN提供的数学类基础计算算子库,实现网络在NPU上加速计算。
C++
994
997
昇腾LLM分布式训练框架
Python
163
196
暂无简介
Dart
983
249
Claude Code 的开源替代方案。连接任意大模型,编辑代码,运行命令,自动验证 — 全自动执行。用 Rust 构建,极致性能。 | An open-source alternative to Claude Code. Connect any LLM, edit code, run commands, and verify changes — autonomously. Built in Rust for speed.
Get Started
Rust
1.08 K
144
华为昇腾面向大规模分布式训练的多模态大模型套件,支撑多模态生成、多模态理解。
Python
145
234
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue3 & Vite、Element Plus 的前后端分离权限管理系统
Vue
1.67 K
978
deepin linux kernel
C
29
16