Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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. 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 (e.g. a billing script or registration script).

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? Like... Is there some sort of nginx thing I can write to make this work? I don't want to run the whole site on a Node.js server either.

like image 977
Alex Bass Avatar asked Oct 21 '22 09:10

Alex Bass


1 Answers

Node.js isn't a server. You can create a server with Node.js using modules like ExpressJS. But Node.js is a runtime environment for evaluating and running Javascript outside of a browser.

So, that means, if you had a hello.js file that contained a single line:

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. 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. In which case, you'd want a node module for Apache like http://larsjung.de/node-cgi/

That said, you still have a "server" running, it's just Apache instead of an ExpressJS node server.

like image 168
Ray Wadkins Avatar answered Oct 23 '22 01:10

Ray Wadkins