Loading .html and .js with websockets - javascript

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.

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

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.

Block access to WebService.asmx in asp.net

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.

Library that auto-updates/reloads the current page when the server changes

I'm looking for a Javscript library, server, etc. that will allow me to automatically reload a web page when the version in the server changes.
Update: I understand the technologies involved, and what it would take to implement this. What I'm looking for is something that's already made. A script I can include in my HTML file that will monitor the server for me. I mean, why reinvent the wheel? :D
Do an ajax call at set intervals to a server side script passing it a timestamp of the latest file, compare that to the timestamp of the file on the server, and if the one on the server is newer, then echo back the contents and reload the page.
You need something like Comet, that lets you send a push message from the server to the client, as soon as a new version is available. The basic idea is to keep an connection open from the client to the server, over this open channel you can send messages to the client which in turn can react to such messages (e.g. by executing JavaScript code to reload the page).
See this example with a PHP backend on how to implement Comet.
The must for this kind of technology is node.js with the socket.io module.
It allows you to use WebSockets, a channel remaining open between the client and the server, and both of them react to changes when a message is sent either way.
When websockets are not available (not using modern browsers), socket.io fallbacks to long-polling ajax.

Download file from FTP via Javascript

I have a got a file server and a web server. They are running on physically different machines.
I would like to download a file from the FTP server via JavaScript. The reason I have to do it via JavaScript is that I have an external application and I can only inject JavaScript into that application.
Basically, I need to specify ftp address username and password. But I am concerning about security as people can view FTP credentials.
What is the best way to implement such scenario?
Thanks for your help
Regards
Javascript only speaks HTTP and WebSockets (on newer browsers), and not FTP. In that situation, keeping it all on the client-side, you'd probably have to write a Flash or Java applet that handles the actual FTP protocol, and interface with Javascript to provide interactivity.
Unless you're planning on redirecting the browser to the ftp site, passing in the username and password? Are you concerned about the users getting the FTP information, or are you concerned with man-in-the-middle attacks sniffing the plaintext FTP credentials?
JavaScript doesn't support FTP. What you need is a server-side or a more robust client-side language to access the remote server.
If by "downloading" you mean "prompt user to save a file from external link" (which basically means open a new window with URL that points to a file) then you can just point user to a script you have control over.
window.open('http://myserver/get_file/filename');
And your server-side get_file script will do all the work of connecting to a FTP and fetching a file
How about creating an iframe and setting the url to ftp://whatever?

Categories

Resources