Simon Yazkaron
Simon Yazkaron

Reputation: 79

(Switching from PHP to NODE) - How to communicate with front-end?

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.

Upvotes: 1

Views: 596

Answers (1)

O. Jones
O. Jones

Reputation: 108796

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.

Upvotes: 1

Related Questions