admin管理员组文章数量:1356286
Last 2 days I spent more time and read 50+ articles and video to understand node.js and after installation now I can see the result in browser by http//:localhost:3000/
But I have confused in many case that I describe below.
I do all of my work in my share hosting server where I my keep my web site: www.myweb
In every article about node.js, they are teaching how to get a result by below code in a browser by http//:localhost:3000/
in local pc server.
test.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://localhost:3000/');
But My Question:
If I use http//:www.myweb/test.js` in my browser, What will be the above code?
In case of local pc we write on npm
node test.js
, But In case of hosting server when any clint open the page likehttp//:www.myweb/test.js
How to work it?In case of php we used include ("head.php") to got something from that page But In this case How to make a call on node.js.
Last 2 days I spent more time and read 50+ articles and video to understand node.js and after installation now I can see the result in browser by http//:localhost:3000/
But I have confused in many case that I describe below.
I do all of my work in my share hosting server where I my keep my web site: www.myweb.
In every article about node.js, they are teaching how to get a result by below code in a browser by http//:localhost:3000/
in local pc server.
test.js
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);
console.log('Server running at http://localhost:3000/');
But My Question:
If I use http//:www.myweb./test.js` in my browser, What will be the above code?
In case of local pc we write on npm
node test.js
, But In case of hosting server when any clint open the page likehttp//:www.myweb./test.js
How to work it?In case of php we used include ("head.php") to got something from that page But In this case How to make a call on node.js.
-
Go to
http://www.myweb.:3000
– Explosion Pills Commented Mar 20, 2015 at 14:29
2 Answers
Reset to default 6Well, what you need to do is understand how http web servers works.
Usually, on your remote machine (your server), you have an instance of a web server (ex : apache) running, which is listening to port 80 (standard port for http requests). It will handle every request made on that port, and manage routing to use the correct php/html file.
Then, it will run the php code server-side, to render an html file and serve it to the server. So the client will not see the php code at all.
Let's talk about Node.js. Node is an application that runs javascript code server-side, and can run an http server with using some modules. But the javascript code will never be shown to your client, he will only get the http response you send him (typically, the html page).
So now, with node.js, you need to do the same as the apache server did, by creating the http server. First, what you have to know is that not that many website host are offering node.js, or even console access. They usually serve the php/html files you put in the configured folder, and that's basically it. What you need is either a virtual machine, or a server on which you can install node.js and run it, or use a node.js hosting service, like heroku or nodejitsu to host your node.js http server.
So, to create the node.js http server, you need to create an http server (as you did in your code), and make it listen to port 80. Now, every http request send to your server will be handled by your node.js instance. Then, you can do anything you want with that request.
I hope I haven't been to messy.
You need to install NodeJS on the server. If this is shared hosting where you cannot install additional software then you will be unable to use NodeJS. In that case contact support of your web hosting pany and inquire about NodeJS support.
On the other hand, if you do have root user or super user rights on a system, you can install NodeJS. For example for on CentOS/RHEL systems you can install using yum with the following mands.
sudo yum install epel-release
sudo yum install npm
For some of the other distributions of Linux: http://ask.xmodulo./install-node-js-linux.html
To access Node applications from your PC to the server, you also need to open a port in the server firewall that your Node aplication uses.
本文标签: javascriptHow to run nodejs in my web site server not my pc local serverStack Overflow
版权声明:本文标题:javascript - How to run node.js in my web site server not my pc local server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743981928a2571117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论