Nodejs as javascript src? - javascript

I was wondering if I can call nodejs as script src="nodeapp.js" and render for example an image or a piece of another javascript retrieved by node app?

No. You cannot do that directly.
A script tag loads a script to be run in the browser in the browser environment.
Node apps run on the server in the server-side node environment.
You can create a server-side app (powered by node if you want) that will respond to a client-side request and produce the image you want to show. But, the server-side code will run on the server-side, not in the browser. Thus you make a request of the server and it does the work server-side and then delivers the result to the client. It does not download the server-side code to the client and run it in the browser.

Related

How to read and write to local text file from a localhost server (preferably using Node's fs module)

I'd like to find an easy way of reading and writing files (stored locally) from my program running on a localhost server. If I use const fs = require(fs) it can't find the Node module from the server. This makes sense. Is there any way of accessing that file from my javascript easily, with similar functionality to the fs module?
It appears that you aren't quite understanding how the client/server architecture works for an http server and the browser.
There's an http server that runs in nodejs. That has access to the local file system using the fs module. This is server-side code that runs in the nodejs execution environment.
There's an http client (the browser). It makes an http request to your server, the server sends back an http response and the browser gets that response and processes it. If the request is for an html page, then the browser gets that HTML, parses it, displays it and then runs any scripts embedded in the HTML page. The scripts running in the HTML page are running in the client/browser (not on the server).
Javascript running in the browser does not have access to nodejs modules like the fs module. It only has access to the libraries that the browser makes available. Absent special browser add-ons (with elevated privileges and associated security risks), scripts running in the browser in HTML pages do NOT have access to the local file system. This is for security reasons as you don't want random web pages that just happen to click on to have access to anything on your local hard drive.
So, you generally do not access local files from your browser Javascript. Instead, you either store data in LocalStorage and fetch the data from that or you store the data on the server (on behalf of the specific user) and that data is either included in the web page (so the Javascript running in the web page can use it) or the data can be requested from the server by the browser Javascript (using Ajax calls).

where should I install axios js? server side or client side?

I am trying to use axios for data loading. As I am new, i got confused where should I install axios js.I tried to install on client side. Is that alright?
Javascript files need to be hosted by a local (to the file invocation, not store) runtime engine. In a browser, for example, that might be the js v8 engine. Remotely, on a server, that server would have to have a local js runtime, presumably Node.js. The point is, if you want the js code to run in a browser, tag it in the html page, the browser will fetch the file from the web server, load it locally, and run the code. Otherwise, you an invoke it on the remote server, if the remote server has a runtime host such as Node, in the same way you invoke anything on a server; with a URI call (an example might theoretically be "https://......com/js-files/axios.js", and depending on if the server is configured to exec the resource, or fetch it, that's what it will do. Axios is isomorphic, so it can either run on the server in Node (if the server is correctly configured), or an HTML document can fetch it via a tag, and run the code locally, in the browser window/DOM. Is that clear?
You can do either or both! They are separate runtimes/environments.
According to the docs, Axios is a Promise based HTTP client for the browser and node.js. So it really depends on where you want to initiate the request.
If you want to do it on the browser consider adding the package in the HTML either via a script tag or a bundled js file:
See installation steps:
https://www.npmjs.com/package/axios#browser-support

Get element by name in express/node

I'm creating an express/node app and I want to get an element by name but the server crashes when I use document.getElementsByName() so how can I get an element by name on express/node?
The document object that has document.getElementsByName() is something that Javascript that runs in a browser can use. Javascript that runs in nodejs cannot use that as the document lives in the browser, not in nodejs.
It appears that you may not quite understand the client/server nature of the web. A user starts up a browser that runs on their computer. When you type an URL into the URL bar in the browser and hit enter, it sends a request to a web server (a different computer usually off somewhere else on the internet). That web server receives the request, looks at what the path of the URL is and any parameters on the URL and then sends back content.
If what the URL represented was an HTML page, then the browser (running on the end-user's computer) takes that HTML page, parses it and displays it in the browser. If there are any references to other resources such as images or scripts, then the browser also requests those from the server.
If there is Javascript in the web page, then the browser runs that Javascript in the browser. It is this Javascript where document.getElementsByName() can be used. This is not nodejs Javascript, this is Javascript meant to run in the web page.
Where people occasionally get confused is that webpage Javascript actually lives on the server's hard drive somewhere. When the browser requests that script or that webpage, the nodejs server gets that request and sends the webpage or the script back to the browser. So, while the webpage script lives on the nodejs server, it does not actually run there - that's just where it is stored so that it can be sent to the browser when requested. When it runs in the browser, it can only use browser APIs, not nodejs APIs.
The Javascript in your nodejs server itself runs on the server. It can only use nodejs APIs, not browser APIs. So, that's why nodejs Javascript cannot use document.getElementsByName(). That only runs in the browser.
Node.js does not run in a browser, thus there is no 'document' element in the first place. If you are asking how to parse an HTML file on the server side that's a different question.

No need to restart the server after making changes in html or javascript. Why?

We don't need to restart the server after making changes in HTML or JAVASCRIPT. But we need to restart it after making changes in Servlet or any server side code. Why?
HTML and JS are interpreted by your browser runtime and hence the changes are reflected immediately on browser refresh. Servlets and server side code usually require compilation and hence requires a server restart. A server restart forces the reload of changed classes. That's why JRebel is interesting (it enables server-side class reloads without server restarts). Hope this helps!
It's not about server-side or client-side, but about the way your server serves your application. You have used the word 'Servlets' so I assume that you are writing in JavaEE.
When your Catalina server launches the application it will load the entire app into the JVM executing the entire server. It does not track file modifications on the disk. If you want Catalina to do that you can checkout this. Why are my JSP changes are not reflected without restarting Tomcat?
Where I disagree the others answers it's about the simplification :
It's not because It's server-side code that you have to reload your server. You could find a variety of languages which tracks files modifications such as PHP for example or even your dear JavaEE as you can read it at the above link.
You can note also that It's not because HTML, CSS and JS are executed client-side you don't need to reload your server. It's because your server configuration read the files on the disk each time they are requested by a client. If you had any cache system you would need to flush it before to see your modified files downloaded to client.
Server code is just a program. When you run a program, its content is loaded into RAM and run. If you update the program, the version on disk is updated, however the old version remains in RAM. You have to close the old version of the server program and run the new one.
HTML and Javascript are rendered on clients. These usually aren't loaded into the server's RAM (except for caching purposes). HTML/JS aren't even part of the server code (although JS can interact with a server, AJAX is a prominent example)
Because HTML and JS executes on client side - in users browser. Instead of server side code. But it's not right proposal to restart server on code change, because server side code execute on each user external request by server.
You code can make some kind of caching to not execute code before some special server side event happens.

Getting a Python server to execute a script

I'm pretty new to web development, so bear with me if these questions seem pretty fundamental.
My use-case is as follows: I am hosting a server using ngrok which allows me to allow clients to securely tunnel to a server I host locally, using a public http(s) URL that ngrok generates.
Currently the server I am running locally is the simplest possible python web server (I literally just invoke 'python -m SimpleHTTPServer 8080').
Let's say I have a file "simplescript" located in the local directory where I instantiate the Python server. I would like to put in php code in "simplescript" so that when somebody fetches the file by using the browser to access "https://ngrokURL.ngrok.io/simplescript", then my server will execute the php code in the script directly and return the return value (if one is generated) to the client requesting my file.
Right now, when I try to do this, the client accessing my file only receives the actual text content of the script, and so it seems that the script is not being executed at all.
Question:
Is my use-case something that can be achieved using the Python simple HTTPserver, or do I need other tools?
How can I make my scripts executable for my server (if that is something I have to do)?
Thanks for the help!

Categories

Resources