So I'm pretty late to the Node.js party. Mainly because nobody invited me... Thanks. That said, I'm starting to work it out. I have come from an ASP classic background so there are a few things I have yet to understand.
If someone can point me in the right direction, that would be great. Thanks in advance.
So, I'm setting up a server the standard way.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
This gives me a nice page at http://127.0.0.1:1337/
. Lovely.
The site I am building resides at /
. Is it possible (don't laugh) to setup the node server to run from a sub folder of my site, let say /api/
?
So then, any queries from client-side scripts can be sent to /api/
rather than http://127.0.0.1:1337/
.
EDIT:
To make things a bit clearer. I am currently running a custom PHP framework at /
, but looking to drop this long term. In the mean time, need to run them in parallel.
EDIT Again, to clarify, I am running everything on my OS X, so apache (MAMP) installation.
So I'm pretty late to the Node.js party. Mainly because nobody invited me... Thanks. That said, I'm starting to work it out. I have come from an ASP classic background so there are a few things I have yet to understand.
If someone can point me in the right direction, that would be great. Thanks in advance.
So, I'm setting up a server the standard way.
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, '127.0.0.1');
console.log('Server running at http://127.0.0.1:1337/');
This gives me a nice page at http://127.0.0.1:1337/
. Lovely.
The site I am building resides at http://newsite.dev/
. Is it possible (don't laugh) to setup the node server to run from a sub folder of my site, let say http://newsite.dev/api/
?
So then, any queries from client-side scripts can be sent to /api/
rather than http://127.0.0.1:1337/
.
EDIT:
To make things a bit clearer. I am currently running a custom PHP framework at http://newsite.dev/
, but looking to drop this long term. In the mean time, need to run them in parallel.
EDIT Again, to clarify, I am running everything on my OS X, so apache (MAMP) installation.
Share Improve this question edited Mar 17, 2015 at 23:39 CommunityBot 11 silver badge asked Nov 19, 2013 at 17:25 jamesmhaleyjamesmhaley 45.5k11 gold badges37 silver badges51 bronze badges 4 |3 Answers
Reset to default 6You have tons of options, but none of them will allow you to use port 80 for your Node application on the same server as Apache+PHP without proxying.
Your two best options are the following:
1) set up a new subdomain - create a new DNS entry for node.newsite.dev, and direct that subdomain to a completely different IP, on a different server (though, technically, you can set up two IPs on the same server, see here), then node can be run on port 80 on its separate server
2) have Apache run on port 80 on /path/to/apache/publicdir/newsite.dev, and have node run on port 1337 on /path/to/node/application/newsite.dev, then you can access your apache files at http://newsite.dev, and your node application at http://newsite.dev:1337
Whatever you do, don't put your node application in a subdirectory that Apache knows about, unless you want to serve those .js files publicly.
EDIT TO RESPOND TO YOUR EDIT:
If your goal is to move to Node exclusively and eventually turn off Apache+PHP, then your best bet is to use a subdomain. The downside is that you'll have to use fully qualified links everywhere. The upside is that when you feel enough of your application is in node, you can do a find/replace (#//(www\.)?newsite.dev#, '//apache.newsite.dev')
and (#//node.newsite.dev#, '//newsite.dev')
, and then when you're totally off of Apache, just shut it down.
You are asking to create a virtual directory and yes you can setup Node.js with PHP but you must do a little reading.
A virtual directory is a website that lives within a folder like www.yourwebsite/myotherwebsite
Here's how to set that up in Apache.
http://httpd.apache.org/docs/current/vhosts/examples.html
Here's how to set that up in IIS.
http://technet.microsoft.com/en-us/library/cc771804(v=ws.10).aspx
You would then need to hook up Node.js with Apache or IIS. Here's more instructions.
Linux: How can I implement virtual directories with node.js and express?
Windows: http://www.hanselman.com/blog/InstallingAndRunningNodejsApplicationsWithinIISOnWindowsAreYouMad.aspx
Only one server can listen on a port at a time. You cannot have Node.js and some other server on the same port.
The best thing to do is set up a different hostname for your other server.
If you cannot do that, the standard way is to proxy requests from one server to the other. This is relatively easy to do, but you didn't specify what server you're running, so it is impossible to be more specific.
http://newsite.dev
already? In that case it's probably easier to use a subdomain, likehttp://api.newsite.dev
where your node application is running. – Max Commented Nov 19, 2013 at 17:40