javascriptphpnode.jsserver-side-scripting

hard time understanding server side script


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!


Solution

  • 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.