I'm unsure if I'm asking the right question here. I apologize in advance for my ignorance, I'm only a year into teaching myself full stack JS and web app architecture and I'm trying to understand this on a granular level. Previous attempts to ask this question seem to offend some devs, so maybe it's a dumb question.
I'm trying to understand how to, or if its possible to, explicitly designate or assign a specific file or block of javascript to process/run/execute on a server vs on the client/browser.
Writing some javascript in a an app.js file and running node app.js seems to still expose that script to the browser, viewable in the Sources tab in Dev Tools.
PHP is interpreted by a server and then sends static HTML to the client. Is this "pre-processing", the equivalent of Server Side Rendering with Javascript App frameworks? Or Is writing a .php file inline with HTML server-side (like you'd see in Wordpress) more equivalent to inline javascript client side? And there is a different method to have JS interpreted server-side?
I've been reading about GENERATE_SOURCEMAP, but that seems more like it's used to hide client-side JS modules.
Other possible wordings of this question
How to not expose server side Javascript to the client?
How to run/execute Javascript on a server vs in browser?
I am NOT ASKING for the definition of server-side vs client-side JS, or for suggestions of server side application frameworks.
Again, I think i've confused myself or missing something very basic. Maybe the only answer is to segment your private web application from your client application and access via API. Thank you for any help.
Summary: Your JavaScript executed by the node daemon on the server side is not visible on the client side, the only thing visible is the output of your JS.
When you create a file with the .php extension you need an executable that executes this file, to return the result to the client to render it.
For example :
create a php file index.php with the following syntax:
<?php
echo '<script>console.log("HelloWorled")</script>'
?>
to run this file, you need the PHP daemon (the php interpreter) to run the file to get this output <script>console.log("HelloWorled")</script>
next; the output will be returned to the client to be execute on client browser;
In the case of PHP this operation is managed directly by the http server such as nginx via this configuration:
server{
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php<version>-fpm.sock;
}
}
which you will find inside the /etc/nginx/sites-available/default file
This rule simply tells nginx to make all files with the .php extension go through the php proxy to be executed and then return only the result of the execution to the client.
You have to imagine the exact same thing with nodejs.
I created an index.js file with the following syntax :
....
process.write( '<script>console.log("HelloWorled")</script>');
....
running the file with node index.js you will just get the output <script>console.log("HelloWorled")</script> which your server (e.g. expressJS) will send back to the client to run it client side to get los same result of the php code.
NOTES :
YOU DON'T NEED TO EXPOSE YOUR JS CODE, YOU NEED ONLY A PROXY TO SERVER IT Like PHP WITH NGINX (read more about expressJS and the other nodejs servers frameworks).
IF you need to expose a client side scripts,images,styles, you need to specify it in your proxy configuration
Well, straight to the point, I have a python script that, after a given URL, scrapes data from a certain website and creates an excel sheet, also, I have a running web server that I made for a user.
How can I run that python script on my backend after inputting the website URL to be scraped in the frontend?
Thanks!
You need to use Node.js to create an http server to receive requests from the front end, and then send a request to the server to execute the script when the front end presses the button.
For how to create an http server, you can take a look at express:
https://expressjs.com/
And then you can use child_process to run python script. https://nodejs.org/api/child_process.html
Suppose, I have a PHP file present on server that contains some text, HTML, CSS, JavaScript, and PHP code.
As per my knowledge any PHP code is executed on server side upon receiving the request for the same PHP file from client(i.e. the web browser).
Rest of the code from the PHP file i.e. HTML, CSS and JavaScript code are executed on client side(i.e. in web browser). In other words it's the job of web browser to execute such code on client side itself.
My question is all the things(i.e. the text, HTML, CSS, JavaScript, and PHP code) are present in the same file that has PHP extension and this file starts execution only upon receiving request for same file from client.
Then, after receiving the request how does the code separation is done and who does it?
By separation, I mean the separation of server side code and client side code. Who and how does this code separation takes place?
Also, in the end I get result of all the code on one single page(i.e. the PHP file I requested). Who and how does this re-combination of output of server and client side code in one single file?
PHP is run on the server, which outputs only content (html, css, javascript, images - whatever isn't php, or is produced by the php code) to the client. All of which are to be handled by the client upon receiving - html and css will be parsed and displayed, javascript will be executed, and every other content handled in its own way. You may see each request from the client in your browser development tools, as well as the server response (each of which has a mimetype, meaning it only has one kind of content). A full, standard webpage usually is sent across several requests, the main html body being only the first of them.
I often hear of the term server-side and client-side programming in regards to web development. They say that server-side and client-side are in a way decoupled from each other. From my understanding, server-side programming makes use of PHP, Rails, Node, ASP.NET, etc., as the technologies and client-side programming makes use of HTML, CSS, Javascript, etc.
Here is where I am fundamentally confused.. From what I know, a PHP file can include HTML, CSS, and Javascript... My question is:
If server-side and client-side programming are indeed separate, why does PHP include HTML, CSS and Javascript? If all of these are done in PHP, the server, where does the client come in? In a typical website run on a PHP server, will there be standalone HTML, CSS, and Javascript files that are not PHP files? Will the client-side developer have have edit the HTML, CSS, and Javascript parts of the PHP file, while the server-side developer works on the PHP part of the file?
Your question sure is a good one many people asked sometime in their lives(web developers).
PHP indeed is a server side script, but the .php extension acts like a normal .html file most of the time.
PHP needs to be a partner with JS and HTML to work.
E.g. A login form. First, the client has completed the form and submitted it. JS then comes in power, using ajax to send your login information to the server(It could be the same document xxx.php, but the server only cares about the php script part).
Then, it sends back a result from the server and may insert a snippet of JS into your login form, where JS empowers and redirect the user from their HTML interface to a new website.
As you can see from the above example, clients and server handles a webpage differently disregarding their file extension. Clients cannot download the PHP source code and the PHP server doesn't care about other than php code themselves.
A single web file is like a port, where clients send information to a php page and the server returns a snippet.
Clients and servers may use one single .php page or can refer to different pages, but the server side webpage is always unaltered
If server-side and client-side programming are indeed separate, why does PHP include HTML, CSS and Javascript?
So it can compactly pack small things inside one web page. Clients view the interface, server executes the PHP code. However, it is not necessary to pack everything into one webpage.
Also, the .php extension can be viewed by clients, so that they know they will interact with the server sometime on that page. Also, .php does not necessary need to include PHP code.
If all of these are done in PHP, the server, where does the client come in?
Clients need to use JS to send information to the server for its response.
On a typical webpage running on a PHP, will there be standalone HTML, CSS, and JavaScript files that are not PHP files?
Yes, files that need not to be parsed by the PHP engine can be named and stored as standalone HTML, CSS and JavaScript file.
Will the client-side developer have have edit the HTML, CSS, and Javascript parts of the PHP file, while the server-side developer works on the PHP part of the file?
I will rephrase your question to be "So the client-side browser can change the DOM, while the server works on the PHP part?". There is no "client sided developer. There is just client sided visitors"
Partially right. Clients download a webpage, not using the same file on the server, the webpage can be altered before sending to the clients. Clients don't get to read the PHP source code, the server is done running the PHP code before sending a webpage to the clients, so the two do not run together. When clients send queries to the server, the server executes nothing but PHP. The .php document is unaltered on the server. After the PHP server has responded, usually, they will send back information to the browser viewing that particular webpage and trigger JS code and change the webpage's DOM, which means the look of the webpage is modified. You can interpret it as "HTML, CSS and JS" being altered.
If all of these are done in PHP, the server, where does the client come in?
The WWW works on a server-client basis.
Web browsers ask web servers for resources. Web servers send those resources to the browser. The browser then interprets them.
When you use server side programming, you just generate those resources programatically instead of reading them from files.
So:
the PHP will run on the server and generate some output
the output it sent to the browser
the browser interprets the output
So the output has to be in a form that the browser understands.
In a typical website run on a PHP server, will there be standalone HTML, CSS, and Javascript files that are not PHP files?
Generally speaking, the CSS and JS will be in static files. The HTML contains the data which is likely to be dynamic (and then generated from PHP) so will probably come from PHP files (although they might use separate template files to get the HTML structure).
Will the client-side developer have have edit the HTML, CSS, and Javascript parts of the PHP file, while the server-side developer works on the PHP part of the file?
There are lots of different ways of working. If you get your server side logic sufficiently separated from from your client side code, then that is a viable way of working.
PHP needs to be executed on the server. The PHP usually determines which CSS/JS/HTML to export to the client. Therefore it is in the PHP file.
HTML, CSS and JS actually is executed on client side. The reason it is in your PHP file, is we need some way of delivering the code to the client. It doesn't just magically appear.
CSS and JS doesn't even have to be in the PHP file. You can use HTML inclusions to let the browser fetch it
eg.
<script src="/loc/of/js/file.js"></script> (JS)
<link href="/loc/of/css/file.css" rel="stylesheet"> (CSS)
Whene you use PHP page, means you want to execute some code in your server at the same time you can include some JS code , or CSS styles, but the server will not execute it.
All the PHP code will be exucuted by the Server.
CSS and JS code will be interprets by your browser because they're front-end code.
All PHP does is that it creates html pages which contain css and javascript code as well and sends it to client. Now how php creates a page differs in application. I hope it helps.
Every time user click specific button, my javascript code needs to send request to the different server, and download a file to my server, than process it.
Is it possible to do?
JavaScript by itself would not be able to do this, as it is a Client-side scripting language, meaning it runs entirely on the user's browser and has little to do with the server itself.
However, it would be entirely possible to do this with PHP (or any other server-side language), and then have JavaScript tell the server to run the PHP script to download and process the file.