1

I want to switch from PHP to NODE. I'm a hobby programmer and did some websites using JavaScript(JQuery), PHP, and mysql.

I'm used to stuff like $.post(), $.get(), fetch(), getJSON() etc... to send/receive data from my php scripts that upload or read data from a mysql database.

And here comes my problem. When I use NODE scripts and try to communicate with them I just get the code back and not the data. The scripts work fine in a console when run with node command.

I guess PHP is always running in my Apache, but NODE is not?!

My question: How can I communicate front-end <-> backend when using NODE instead of PHP? Thanks for answers in advance.

  • 1
    This is really too broad a question for here, you should rather go find yourself some beginner tutorials on the matter, such as https://www.sitepoint.com/build-a-simple-web-server-with-node-js/ – CBroe Apr 22 '20 at 14:21
  • 1
    you need to start a http server in node.js, you can also use express server which is built on top of http module. watch a tutorial about node.js on youtube, almost any tutorial on node will include express server too – vikrant Apr 22 '20 at 14:22
  • So my apache wont do? – Simon Yazkaron Apr 22 '20 at 14:23
  • 1
    no, you need a compiler to compile javascript files and serve the result to a browser. node js uses the Chrome's V8 javaScript engine for that. i don't apache has any javascript engine – vikrant Apr 22 '20 at 14:26

1 Answers1

1

As comments have said, nodejs replaces apache. If your nodejs program is called server.js, you need to run this to get your server up and running. node server.

If you also have apache running on the same port, nodejs won't start. It will complain that the port is already in use. But you may not see that problem, because the various nodejs sample programs use port numbers like 3000, whereas apache typically uses port 80.

Switching from apache to node involves also switching your way of thinking about how web servers work. apache is a file server with, well, a patch (hence the name), to run php scripts instead of just sending their source. On the other hand, nodejs is a non-file server. If you want it to serve static files you have to build in the "static" middleware code to do that.

At the heart of apache is the idea that http://example.com/a/b/c.html looks up a file in your server file system at <<root>>/a/b/c.html. Nodejs is not inherently a file server. In it, /a/b/c.html is a text string called a route. Only if you bind that route to a file system with "static" middleware (or some other code) does it deliver files from the file system.

Your browser code should work the same. You may have to change your route names in your browser code from whatever.php to just whatever, or write your nodejs code to accept the .php stuff.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
  • 1
    All correct, although running a node server doesn't stop you from proxying it through apache. eg https://stackoverflow.com/questions/14259321/apache-node-js-mod-proxy-how-to-route-one-domain-to-3000-and-another-to-8 – Jesse Bugden May 11 '21 at 00:31