I have a simple html page being served by express on a node.js server and in the client side javascript i want to access some server side variables. I tried googling around for a solution but didn't see a straight forward solution without using some sort of templating engine.
So if i need to access a variable on the server from a client side JS served from the same host, what do i need to do?
To access the variable on the client, you'll need to expose it inside of a script tag, there are a few node modules that can help you with it (ex: express-state) https://www.npmjs.com/package/express-state, but you'll need a templating engine to generate the html required for it. You can use a variety of templating engines when working with express. If you use express generators straight out of the box it should come with jade and you can use different options on the generators to use other templating engines. See here for express generator options http://expressjs.com/en/starter/generator.html
Create an API to expose your variable:
app.get('/myvar', function(req, res){
res.send(varYouWantToSend);
});
Then in the client-side make a call for that API with a http GET request. The url for the API would be www.yoursite.com/myvar in this example.
You could try using this lib:
https://github.com/siriusastrebe/syc
and if you want to implement yourself look into Ajax. Jquery have a pretty simple ajax wrapper (http://api.jquery.com/jquery.ajax/). offcourse you need to open corresponding urls on the server to return / set the values.
Try socket.io http://socket.io/ it uses websockets on modern browsers and gracefully degradate to older technologies like AJAX with older browsers, leaving user experience the same.
Check out details here Web Socket support in Node.js/Socket.io for older browser
Related
Ive been looking around and i cant find an answer for creating a Pure JavaScript Proxy server without node.js. If anyone has an answer please comment. I’m wondering if its possible because i want to set up my own proxy server without node.js so its fully web based.
EDIT 2
So now that i know that i cant directly create a proxy server on the web, is there a way to do it in Java? (Or a similar language)
You cannot accomplish having a proxy server (or any type of server) in the browser. Reason is that proxy servers require a static host that would map data to and from the server.
You will at least need to have a client/server type architecture to accomplish what you're trying to do.
Take a read at: https://en.wikipedia.org/wiki/Proxy_server
I'm attempting to create a NodeJS + Express (currently + Handlebars) app that serves web pages using data from FirebaseIO. However, I have a personal preference for programming in PHP, and, in many cases, it's more optimal for my use case, mostly because of its inline style. FirebaseIO, though, does not support PHP and it's REST API only does database requests (and not other things such as storage or Auth).
What I am interested in doing is have NodeJS + Express receive requests, then pass them on, along with variables Node has retrieved from Firebase, to PHP. PHP would process the request and the variables given it by Node and return a web page which would be sent back to the client via Node.
I'm looking for something extremely stable though (I noticed a couple libraries on GitHub that promise to do this, but they all seemed to be in beta or experimental phases).
If it's not possible to do what I've described, is there any other way to have PHP generate the web pages using variables attained by NodeJS? I'm guessing one way is to add Node on another port of my server and query it using CURL, but wouldn't this have implications for the speed of requests?
Please elaborate and suggest as many paths forward as you can think of.
I am building a simple web service using node js, I only use the built in web server feature using:
var server = http.createServer(handleRequest);
I added a caching mechanism using node-cache, but that's it, I run the script via PM2 to make sure it's always running. Is this a safe and good practice to do? I saw some posts mentioning using nginx as reverse proxy server, won't that add an extra step that will slow things down?
Thanks!
It's recomended to use nginx as a frontier in production for secure reasons, load balancing, static content output and administration issues. In some cases it could be used for API versioning.
Could ajax be used on the client side javascript to function as an in-the-background web spider? You'll have to excuse the vagueness of this post because I really have no idea where to begin technically in terms of the code and there is nothing online about a program like this.
You can use a cors proxy type script in order to do these Ajax requests client side via javascript. Look on Github for 'cors proxy', and set that up in your Node.js environment, then pass all of your ajax calls client side through this proxy.
Yes its possible but with some restrictions, meant only to be done from a specially-configured browser, not for arbitrary users to just run:
For chrome, open it using the command-line parameter --disable-web-security.
now you can do cross domain and such.
I assume you just want it for using yourself as a server and not on a public web page.
You cannot make ajax requests to different hosts, so no.
I have an application on my server that is called leaf.exe, that haves two arguments needed to run, they are: inputfile and outputfile, that will be like this example:
pnote.exe input.pnt output.txt
They are all on the same directory as my home page file(the executable and the input file). But I need that a JavaScript could run the application like that, then I want to know how could I do this.
I'm using just Apache, I don't have any language for web installed on it. My goal is to do a site using just JavaScript, without the help of anyother language than it, HTML and CSS.
You would need to make an Ajax request to the server - the server would then have a handler that would then invoke the executable with the appropriate parameters.
Without know which web server technology you are using, it's harder to give a more concrete answer (ex: ASP.NET, PHP, Ruby, etc).
EDIT: If you're talking about doing this without any kind of server side resources, then this is impossible, and for good reason. Think of the security exploits!
Any other way to this without using other languages that need to be installed on the server?
No, but you almost certainly already have languages on the server. If it's a Linux, BSD or OSX server you've got shell script; if it's a Windows server you've got JScript and VBScript via Windows Scripting Host (using a cscript.exe hashbang).
JavaScript is for Client Side of a web application, so you won't be able to directly use javaScript to access server side files. As mentioned by Tejs, you should use Ajax to make a call to server side and then use appropriate server side routine to do the task.
Even at client side, most browsers don't allow accessing of any resource( e.g files) by javaScript code.
For server side javascript in Apache you could use Sun ONE Active Server Pages, formerly known as Chili!Soft ASP. For an IIS server, javascript is plainly available as asp-language.
Look into Rhino and node.js. I dont know a lot about this, but thats a route you can use for serverside javascript.