gpt4 book ai didi

python - 使用 python 和 node.js

转载 作者:搜寻专家 更新时间:2023-11-01 00:18:52 24 4
gpt4 key购买 nike

我用 python 编程。我几个月前就开始了,所以我不是“大师”类型的开发人员。我也了解 HTML 和 CSS 的基础知识。

我很少看到关于 node.js 的教程,我真的很喜欢它。我无法根据 html 和 css 的知识创建那些表单、栏、按钮等。

我能否使用 node.js 创建用户在浏览器上看到的内容并使用 python 编写如果有人按下“提交”按钮会发生什么?例如重定向,sql写入和读取等。

谢谢

最佳答案

您可以在 Node 服务器的后端调用 python 脚本,以响应用户的按钮点击。为此,您可以使用 child_process 包。它允许您调用安装在您机器上的程序。

例如,当用户在/reg 页面上发布内容时,如何运行脚本:

app.post('/reg', function(request, response){
spawn = require('child_process').spawn;
path = "location of your script";
// create child process of your script and pass two arguments from the request
backend = spawn('python',[path, request.body.name, request.body.email]);
backend.on('exit', function(code) {
console.log(path + ' exited with code ' + code);
if(code==0)
response.render('success'); //show success page if script runs successfully
else
response.redirect('bad');
});
});

Python 必须与您需要的其他 Python 库一起安装在您的系统中。它不能响应/重定向到 Node 的请求,否则你为什么要使用 Node 。在罗马做到入乡随俗。在 node 中使用 JavaScript,调用外部程序不如使用 JS 库快。

关于python - 使用 python 和 node.js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15315984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com