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.
Related
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).
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.
I have built a simple asp.net web application running asynchronously, using WebService.asmx file to access the database. I need to secure the file so clients won't be able to access it, but only the javascript code through an http request. (the file is located on domain.com/WebService.asmx).
I read that people suggest using a user authentication system yet this is not applicable (because it can be easily manipulated).
My final goal is to block the access to the WebService.asmx file from anywhere but the javascript file.
I'm using visual studio 2013 but the app will run on a Windows Server 2012.
In short, you can't.
Javascript runs on the client, in the browser.
Calling a asmx webservice through Javascript is no different visiting the URL in your browser (or any other client - Postman / Fiddler for example)
The server won't know any different.
Secure it appropriately.
I have the idea of having something like a web server but without a web server. Instead I want to use websockets. Is for internal use.
The user will have a basic webpage which will only open a websocket connection and should receive an .html, and immediately navigate to it.
Also it should be able to load a .js with helper functions for this new html.
I saw here something to load an image file
http://liamkaufman.com/blog/2012/02/11/12-pushing-files-to-the-browser-using-deliveryjs-socketio-and-nodejs/
but I don't know how to navigate to a received .html file and how to execute a received .js file.
Do not try to convince me of using a webserver or other technologies, I have my reaasons for doing this :-)
Thanks
Using a websocket inherently means you need to use a server. A websocket is be definition a connection between the client and the server.
From here
WebSockets is an advanced technology that makes it possible to open an interactive communication session between the user's browser and a server. With this API, you can send messages to a server and receive event-driven responses without having to poll the server for a reply.
Regarding running a javascript file that you have downloaded, you can use eval, or you can create a new script tag on the body to load your 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.