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!
Related
Is there a way to run nodejs commands outside the terminal?
I mean, in order to run a nodejs command like { gulp watch }
I have to open nodejs command prompt and type gulp watch
Is there a way to run this command outside of the terminal from a web page, for example, by pressing the button to run the task directly
I want to build a site and use the great features provided by nodejs packages but the user will not use the command line
I'm looking for a way to do it with nodejs running in the background
It's unclear what you're asking:
Is there a way to run this command outside of the terminal from a web page
You don't need a terminal, for example you can run a node.js app in a window with electron.
I want to build a site and use the great features provided by nodejs packages but the user will not use the command line
A 'site' just transmits files to a browser, and browsers generally don't let web sites run code on a client's computer for security reasons, that would be insanity.
To have a public site that runs code on a client's computer, you would need some app outside your site the client would have to install, and communicate with the web site in some way.
A way to do this would be to have a client application that the user installs and that listens for requests from the server, generally termed an 'agent'. The agent would listen for requests for the server. The client's browser contacts the server and the server sends a request to the agent.
Another way would be to have the client app be a server itself, and connect using something like jsonrpc directly from the client's browser. Bitburner for example uses jsonrpc to connect to an app running on your local computer to sync files on your computer with files inside the game.
In both scenarios the client has to install and run an application outside the browser.
You can implement this like a online code compiler.
Suppose you have a button in the front end and you need to run some node.js stuffs, right?
Create a backend server (Nodejs or any other)
Write an api for accepting the request. This API will be called on button click.
if you want to run a static code for all button click, then keep the node.js program as a file in the server side.
Else accept the code snippet in the request body from the user, you will get snippet in the server side. Generate a file and save it.
Now you have the code snippet as a file in the server side. Make sure that, you are saving the file with proper extension (Like .js, .py etc).
In Node.JS, you can use exec from child_process to run the file from server side which result output.
You can return the output to user.
child_process.exec
For an example:
The user is not using command line in this method. Suppose you think about an online code compiler.
(The online compilers are webpages where you write your code and these codes are sent to the server which has the compiler built)
You select language there, python, cpp or nodejs.
Then you are typing your code in text area.
Click RUN button, What happens there?
It simply pass the selected language and code snippet written by user to the backend server.
In the backend, it may be in Python or java or Nodejs etc. Request will reach in server. From the request body we will get the code snippet write? In Node js , there is a exec method from child process, this method runs a command in a console and buffers the output.
This is purely a backend stuff and the user won't aware about what happening in the server.
Simply what this video, hope you will get the idea. Link also updated in answer.
Youtube link for online code compiler
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
I'm making a new website with HTML, CSS, js for frontend and Java for backend (I don't know Java, my friend will do the java part) and I need to use frontend technologies (languages/frameworks) to use localhost
I am unable to find frontend technologies for this purpose. I have already tried python -m SimpleHTTPServer and node.js but as these are backend I can't use them. Also, I can't change my backend language
The important thing here is that you need to understand the terms backend and frontend.
A website is by definition something that can be loaded from a server with HTTP and it made of HTML and maybe CSS and/or Javscript.
Now by definition every code that runs on that server is the backend. All code that runs in the Browser of the user is considered the frontend. If the website only contains HTML and CSS that is generated by the server it does not make much sense to do this seperation. This means we only talk about frontend and backend if we have both.
This also means that the frontend code, that runs in the browser, must be delivered by the backend. A frontend without a backend can not exist.
Lets look on a little example. If a user enters example.com in the browser the browser will make a HTTP GET request to example.com. The server (and so the backend) will respond with a HTML file. This file now could embed a javascript file with <script src="/code.js">. Now the browser does another HTTP GET to example.com/code.js and the server (and so the backend) returns that file. In that file may be some frontend code that is executed in the browser.
Now maybe this file wants to load some data from the backend. So it does a fetch('/api/data). The browser does anotherHTTP GETtoexample.com/api/data` and the backend has to respond. Now while the backend was relativly dumb up to this point and only delivered files it now could actually execute some logic. Maybe load the data from a database or such. It then sends the data to the frontend which then can use that data to do something.
This means in production your backend has to deliver the frontend code. In this example the initial HTML file and the code.js file.
So what you want is by definition impossible. A frontend runs in the browser and so can not deliver code on localhost. If it could it would become a backend.
Now while its common that the real backend delivers the frontend code on production its not common for development. Its very common to have a seperate minimalistic server that is only used for development. python -m SimpleHTTPServer is such a tool and so would do the job.
Sometimes this is also done in production. Then your backend is for example on example2.com and your frontend is delivered by example.com. But this essential means that there runs a second backend just to deliver the frontend. Usually for production this is a full fledged web server like nginx, apache or IIS (you'll need CORS, see below). In contrast to them tools like python -m SimpleHTTPServer should not be used for production.
Now this means basically you can just use any kind of backend to deliver your frontend for development. Later you will give your code to your backend developer and he will then deploy it with his backend. However there is one open question:
How will your frontend and backend communicate?
In production your frontend can just fetch something and it will work if your backend delivers your frontend. But for development (and so testing) you probably already want to use the backend without actually starting it on your computer. For this basically there are two ways.
First your development backend could proxy unknown requests to the backend. This means if your real backend runs on example.com and you start a development server on localhost that server will proxy all requests that are not an existing file to example.com. So if you request localhost/code.js and the file code.js does exist your server will respond with that file. If you request localhost/data and you have no file named data your server should do a request to example.com/data and return that response. This is very common. Depending on the tools, libraries and framework you use for frontend development they have a integrated development server with that capability. For example if you develop with ember.js you can do ember server --proxy=http://example.com. And it will run a server on localhost:4200 with exactly that behavior. Other tools like create-react-app allow the same.
Second you can use CORS. This must be implemented by the backend and allows a frontend from another server to access the backend. This is also needed if you run your frontend and your backend from two different servers as I described before.
If the backend implemented CORS correctly you can just do fetch('example.com/data) to get your data, and this Javascript must not be delivered by example.com and it will just work. However CORS complicates security.
Maybe i'm misunderstanding how Node.js works but, I would like to use it just as a server backend for a web app, without it running as a service/listening a port.
I'm willing to hear ideas to better solve the issue, this app will only be available on our intranet.
Example of what i'm thinking :
backend server.js :
function connectDb(usr, pwrd){
//Some npm package code to connect to a db
return console.log("Sucessfully connected")
}
frontend javascript.js :
require("server.js")
$(".connect.button").on("click", function(e){
connectDb($(".connect.user").text(), $(".connect.pwrd").text())
})
There are two different aspects with your question and code example on which you could work to get a better understanding of the ecosystem.
Client / Server
When a client wants to get some resource from a server, it connects to a specific port on that server, on which the back-end application is "listening". That means, to be able to serve resources coming from a database, you must have a Node process listening to a port, fetching the requested resources from the database, and returning them. The perfect format for that kind of data exchange is JSON.
To get a better understanding of this process, you may want to try and write a simple Node app sending a piece of JSON over the network when it receives a request, and try to load it with an XHR in client code (for example with JQuery's AJAX method). Then, try and serve a dynamic piece of JSON coming from a database, with a query based on the request's content.
Module loading
require("server.js") only works in Node, and can't be used in JavaScript that is running in a client's browser (Well, at least for now. Maybe some kind of module loading could be normalised for browsers, but that's another debate.).
To use a script in a client browser, you have to include it in the loaded page with a <script> tag.
In node, you can load a script file with require. However, said script must declare what functions or variables are exposed to the scripts that require it. To achieve it, you must export these variables or function setting module.exports.
See this article to get some basic understanding, and this part of Node docs to master all the details of module loading. This is quite important, as this will help you structure your app and avoid strange bugs.
For one thing, node itself isn't a web server: it's a JS interpreter, which (among other things) can be used to write a web server. But node itself isn't a web server any more than Java is.
But if you want things to be able to connect to your node program, in order to do things like access a database, or serve a webpage, then, yeah, your program needs to be listening on some port on the machine it's running on.
Simply having your node program listening to a specific port on your machine doesn't mean that anyone else can access it; but that's really a networking question not a programming question.
i perl script that reads the network traffic and prints out payloads of packets of specific length back onto the terminal. and for this to happen, the script should be initiated by root or else the packets will not be read.
i am using pcap in perl for that purpose.
now i have a webpage on my webserver. can i execute that perl script from my webpage and get the output that is being printed on the terminal into my webpage?
i am trying to use javascript to serve this purpose but i dont know how its done.
i tried googling it and all i got was ssh user#remote "perl pathto/perlfile.pl"
can someone help me out with this please?
To initiate a command from a web page, issue an AJAX call to another CGI script running on your webserver.
AJAX call can return the results of running your command (typically in JSON or XML format) and then call up some JavaScript handler to render that data in your page's DOM.
To run something as root on the web server's unix machine locally, you will need an suid script to be called from your CGI script mentioned in the first bullet (since web server and all the scripts it runs are typically run as non-root for security reasons).
To run something as root on ANOTHER machine, you have correctly found that you need to use ssh (from that same CGI script): ssh root#your_remotehost "perl pathto/perlfile.pl"
You will need to set up the ssh keys correctly between 2 systems so ssh doesn't ask for a password.
It is possible that you will NOT be able to ssh as root without a password when running as a non-root user; in which case you would need to:
ssh as the same user as your CGI script runs as
Have the command executed on remote server run the suid script, as described above for local case.
If you need further guidance, you will need to post specific code snippets and error messages or troubleshooting details so we can assess what the problems are.