简体   繁体   English

使用python和node.js

[英]using python and node.js

I use to program on python. 我曾经在python上编程。 I have started few months before, so I am not the "guru" type of developer. 我几个月前才开始,所以我不是“专家”类型的开发人员。 I also know the basics of HTML and CSS. 我也了解HTML和CSS的基础知识。

I see few tutorials about node.js and I really like it. 我看到一些有关node.js的教程,我真的很喜欢。 I cannot create those forms, bars, buttons etc with my knowledge from html and css. 我无法使用html和css的知识来创建这些表单,栏,按钮等。

Can I use node.js to create what user see on browser and write with python what will happen if someone push the "submit" button? 我可以使用node.js来创建用户在浏览器中看到的内容并使用python编写如果有人按下“提交”按钮会发生什么? For example redirect, sql write and read etc. 例如重定向,sql读写等。

Thank you 谢谢

Node.js is a serverside JavaScript environment (like Python). Node.js是服务器端 JavaScript环境(例如Python)。 It runs on the server and interacts with the database, generates the HTML that the clients see and isn't actually directly accessed by the browser. 它在服务器上运行并与数据库进行交互,生成客户端看到的HTML,而浏览器实际上并不直接访问它们。

Browsers, on the other hand, run clientside JavaScript directly. 另一方面,浏览器直接运行客户端 JavaScript。

If you want to use Python on the server, there are a bunch of frameworks that you can work with: 如果要在服务器上使用Python,则可以使用许多框架:

You can call python scripts in the back end at the node server, in response to button click by user. 您可以在节点服务器的后端调用python脚本,以响应用户单击按钮。 For that you can use child_process package. 为此,您可以使用child_process包。 It allows you to call programs installed on your machine. 它允许您调用计算机上安装的程序。

For example here is how to run your script when user POST's something on /reg page: 例如,这是当用户POST在/ 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 has to be installed in your system, along with other python libraries you will need. Python必须与所需的其他python库一起安装在系统中。 It cannot respond / redirect to requests to node, else why would you use node then. 它不能响应/重定向到对节点的请求,否则您为什么要使用节点。 When in Rome, do as the Romans do. 在罗马做到入乡随俗。 Use JavaScript in node, calling external programs is not as fast using JS libraries. 在节点中使用JavaScript,使用JS库调用外部程序的速度并不快。

I think you're thinking about this problem backwards. 我认为您正在反向考虑此问题。 Node.js lets you run browser Javascript without a browser. Node.js使您无需浏览器即可运行浏览器Javascript。 You won't find it useful in your Python programming. 您不会在Python编程中发现它有用。 You're better off, if you want to stick with Python, using a framework such as Pyjamas to write Javascript with Python or another framework such as Flask or Twisted to integrate the Javascript with Python. 如果您想坚持使用Python,则最好使用Pajamas之类的框架来使用Python编写Javascript,或者使用Flask或Twisted之类的其他框架来将该Javascript与Python集成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM