-2

I learned that php and node.js are both server side script.

I'm having hard time understanding the connection between node.js and running javascript on web browser. So can we not access files and database with javascript on web browser? How are server-side-script and client-side-script connected(or how can we connect them)? Or should I think of them as completely different things?

One more confusing fact is that I learned that I can get access to database and files using php within html file, but what about node.js?

Thank you in advance!

  • 1
    From the client's perspective it makes no difference what code is running on the server. The communication between the browser and the server is all HTTP requests/responses. Server-side code executes on the server, the result of that code is content to send to the client. That content may *contain* lots of JavaScript code for the browser to execute, but it's all just raw content as far as the server is concerned. When the browser receives that response content, it executes client-side code. – David Dec 19 '18 at 13:41
  • `I can get access to database and files using php within html file,` No, you have a PHP file that renders an HTML file, you can't run PHP in the browser. The same would be for NodeJS, it will have access to a database, that it will then render say using express or koa, that returns an HTML / CSS / JS etc. file to the browser. – Keith Dec 19 '18 at 13:44
  • Possible duplicate of [What is the difference between client-side and server-side programming?](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) – faintsignal Dec 19 '18 at 13:56

2 Answers2

4

I'm having hard time understanding the connection between node.js and running javascript on web browser.

There isn't one.

JavaScript is just a programming language. You write software in it. You can run that software in different places. What you can do with that software depends on where you run it.

I could write some JavaScript that would turn the lights in my house on and off. If I didn't have hardware which would let me do that, then I couldn't.

JavaScript running in a browser can be written to do things that the browser makes possible.

JavaScript running in Node.js can be written to do things that Node.js makes possible.

How are server-side-script and client-side-script connected(or how can we connect them)?

JavaScript running in a browser can make HTTP requests to an HTTP server (and process the response from an HTTP server).

JavaScript running in Node.js can be an HTTP server.

One more confusing fact is that I learned that I can get access to database and files using php within html file, but what about node.js?

No, you can't.

If you run PHP through a web server, then PHP will automatically generate HTTP response headers to say it is outputting HTML (you can override that).

In the PHP language, code outside of <?php ... ?> will just be streamed direct to the output.

This means you can type an HTML document inside a PHP program, but outside of <?php ... ?> and it looks like there is PHP inside an HTML document. Really, it is a PHP program that generates an HTML document.

JavaScript does not work that way. It doesn't have a "Real JavaScript mode" (like <?js ?>) and a "Stream directly to output mode".

There are plenty of template engines that you can use, including EJS (which lets you embed JS inside a template). I lean towards nunjucks myself.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
1

I would suggest you read some articles about differences between client-side and server-side technologies. Here is a good explanation of the basics What are the differences between server-side and client-side programming?

I think your are thinking in terms of webdeveloping.

Node.js allows to run javascript servers-side. In order to expose database and files to the client (your browser for example) you would need to create a connection in your node.js application. You would also something to serve up files and expose the data to a browser, for example express.js.

In order to show the data on a webapp you would either render your html serverside or make ajax/http request on your client application through javascript.

In PHP you would for example connect to a MySQL database and render some html with the data that you retrieved from the db.

TietjeDK
  • 1,167
  • 2
  • 15
  • 43