Reputation: 40
I want to execute a nodejs script from a html file. I'm trying browserify but i'm getting many errors like "http.createServer is not a function".
Is there any alternatives to browserify?
Upvotes: 0
Views: 1263
Reputation: 2873
You cannot execute node.js scripts on the client side.
Actually, that statement isn't entirely true. If a script contains only code that both the browser and node.js can run, or can somehow detect which environment it's running in and switch over to code friendly to that environment, then it will work. But a script is limited to the capabilities of the environment it's running in, so calls like http.createServer()
will not function in the browser. It's not defined in the browser, and probably never will be. That's too dangerous from a security perspective.
What you can do is create a server-side API in node.js, and have the browser call it through AJAX. The script serving up your API will be running in node.js, so it can do anything that node.js can do. However, because that code isn't running in the browser, it won't be able to affect the browser, except by whatever it returns.
I suspect that you actually need two scripts -one on the server and one on the client- that communicate with each other. How you set up that communication is up to you, but if you're going to be starting servers in response to AJAX calls, then you're going to need to be very careful about security. Still, this is doable; apps like webmin are common.
Upvotes: 1
Reputation: 7343
Starting a server on browser won't be possible.
No sane browser would allow that as it can lead to drastic security loophole and attacks.
Upvotes: 1
Reputation: 943128
No alternative to browserify is going to let you do things which are simply impossible in a browser environment … including starting an HTTP server.
Upvotes: 4