A
A
anonrab2014-10-29 10:17:24
PHP
anonrab, 2014-10-29 10:17:24

Can NodeJS work like PHP?

PHP is great because you can write a script, put it in a directory and forget it. In this case, the script will live as long as the server is running. Again, it is convenient to make small changes. Opened the file on the server, changed one line and the script is already updated.
With Node, things are not so rosy. The script must be run on the server. The running script may crash/hang. The script stops running after a server restart. Editing the script is not applied automatically, after editing, you must restart the script.
Is it possible to use Node like PHP - so that there is a www directory, script.js in it, and when server/script.js is accessed , this script is called, and inside the standard function (req) {}, into which request parameters are passed?

Answer the question

In order to leave comments, you need to log in

7 answer(s)
K
Konstantin Kitmanov, 2014-10-29
@anonrab

Certainly. All you need is a CGI -enabled http server (like Apache) and write scripts with the #!/usr/bin/env node. They even did something like this: larsjung.de/node-cgi/.
Not exactly like PHP, because mod_php works a little differently, but the effect is the same: slow, the interpreter starts from scratch every time and other charms of outdated solutions :)

A
Alex Kheben, 2014-10-29
@zBit

Run Node applications via grunt with autoreload configured. As a result, you get what you wanted: you changed the file, and the server will restart, you will immediately see the changes.

_
_ _, 2014-10-29
@AMar4enko

Well, write in PHP, what's the problem?

D
Denis Pushkarev, 2014-10-29
@rock

Maybe. Just don't - all the pluses are lost and Node.js turns into the same dull shit as PHP - the script initialization time is too long. The program in Node is exactly the same as in PHP and any other language. It's just that usually PHP programs interact with the web server via the cgi protocol, while Node programs run their own web server and receive either requests directly or from the main web server. No one bothers to put the script in a folder, write the interpreter in the first line of the script, write the ability to run in some kind of Apache and output data via console.log instead of echo .

V
Valentin, 2014-10-29
@nowfine

hey hey @anonrab take it easy.
if you take an unbiased look, then the php script must also be run. Previously, handles, and then taught to do this by a web server.
So for the node, you just need to find the last one and delegate the launch of the node to him.
in this crown is not whimsical.
cgi from Apache or Yndzhayniks - too.
There are also init scripts at system startup, there you can even make your own handler that will restart your node. And you can even write a node that will restart itself, update the line or send a letter.
ps. broken link to script.js
pps. And php is bad for everyone, except what the hell still works "as long as the server is running".
and yes, I forgot the most important thing -thatextramile.be/blog/2012/01/hosting-a-node-js-si...

S
Sergey, 2014-10-29
Protko @Fesor

I think you just don't need Node.js, you need PHP, since your approach takes all the benefit from node.js. If you need performance - HHVM or HippyVM.

A
Andrey Zenkov, 2014-10-31
@andreyzenkov

Questions like this blow your mind! Please, stop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question