简体   繁体   English

是否可以在不运行服务器的情况下将Node.js用作PHP文件?

[英]Is it possible to use Node.js like a PHP file without running a server?

I'm getting into Node.js at the moment and love how I can use my front-end knowledge to create server-side applications. 我现在正在进入Node.js,并喜欢如何使用我的前端知识来创建服务器端应用程序。 However, the thing I love about PHP is you can just post to or execute/open a file, and it is automatically ran by the server. 但是,我喜欢PHP的东西是你可以发布或执行/打开一个文件,它会由服务器自动运行。 The thing I don't like about PHP is the syntax and the speed - I want to use Node.js and Javascript for all of my server-side functionality (eg a billing script or registration script). 我不喜欢PHP的是语法和速度 - 我想使用Node.js和Javascript来实现我的所有服务器端功能(例如计费脚本或注册脚本)。

So... Is there a way I can run a Node.js application as one would with a PHP script, and by that I mean without creating a constantly running server and having to run "node app.js" in the terminal for every script? 那么......有没有办法可以像运行PHP脚本一样运行Node.js应用程序,我的意思是不创建一个持续运行的服务器,并且必须在终端中运行“node app.js”脚本? Like... Is there some sort of nginx thing I can write to make this work? 就像...有什么nginx的东西,我可以写这个工作吗? I don't want to run the whole site on a Node.js server either. 我也不想在Node.js服务器上运行整个站点。

Node.js isn't a server. Node.js不是服务器。 You can create a server with Node.js using modules like ExpressJS. 您可以使用ExpressJS等模块创建具有Node.js的服务器。 But Node.js is a runtime environment for evaluating and running Javascript outside of a browser. 但Node.js是一个运行时环境,用于在浏览器之外评估和运行Javascript。

So, that means, if you had a hello.js file that contained a single line: 所以,这意味着,如果你有一个包含一行的hello.js文件:

console.log("Hello World");

and ran 跑了

node hello.js

at the command line in the same directory as your script, you'll see "Hello World" spit out. 在与脚本相同的目录中的命令行中,您将看到“Hello World”吐出。 And the node process would exit. 并且节点进程将退出。 No "sever" run at all. 根本没有“服务器”运行。

==================== ====================

Edit: 编辑:

Or, I misunderstood your question and you're wanting to run node js scripts using Apache in the same way Apache executes php scripts. 或者,我误解了你的问题,你想要使用Apache运行节点js脚本,就像Apache执行php脚本一样。 In which case, you'd want a node module for Apache like http://larsjung.de/node-cgi/ 在这种情况下,您需要一个Apache的节点模块,如http://larsjung.de/node-cgi/

That said, you still have a "server" running, it's just Apache instead of an ExpressJS node server. 也就是说,你仍然在运行“服务器”,它只是Apache而不是ExpressJS节点服务器。

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

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