Node.js Server running from a sub folder

javascript, node.js

Solution

You 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.

Problem

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.

Original source

Related problems