Node.js服务器从子文件夹运行
所以我对Node.js派对来说已经很晚了。主要是因为没有人邀请我... 谢谢。那就是说,我开始研究它了。我来自ASP经典背景,所以有一些我还没有理解的事情。
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/');
这给我一个很好的页面 http://127.0.0.1: 1337 /
。可爱。
This gives me a nice page at http://127.0.0.1:1337/
. Lovely.
我正在建设的网站位于 http://newsite.dev/
。是否有可能(不要笑)设置节点服务器从我的网站的子文件夹运行,比如说 http://newsite.dev/api/
?
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/
?
那么,来自客户端脚本的任何查询都可以发送到 / api /
而不是 http://127.0.0.1:1337/
。
So then, any queries from client-side scripts can be sent to /api/
rather than http://127.0.0.1:1337/
.
编辑:
到让事情更清楚一些。我目前正在 http://newsite.dev/
运行自定义PHP框架,但希望放弃这个长期。与此同时,需要并行运行它们。
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.
编辑
再次,为了澄清,我正在运行我的一切OS X,所以apache(MAMP)安装。
EDIT Again, to clarify, I am running everything on my OS X, so apache (MAMP) installation.
您有很多选项,但它们都不允许您在同一台服务器上为您的Node应用程序使用端口80作为没有代理的Apache + PHP。
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)设置一个新的子域 - 为node.newsite.dev创建一个新的DNS条目,并在另一台服务器上将该子域指向完全不同的IP(但从技术上讲,您可以在同一台服务器上设置两个IP,请参阅这里),然后节点可以在端口80上运行在其单独的服务器上
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)让Apache在/path/to/apache/publicdir/newsite.dev上的端口80上运行,并在/ 13上的端口1337上运行节点path / to / node / application / newsite.dev,然后您可以访问 http://newsite.dev 上的apache文件。 ,以及 http://newsite.dev:1337
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
无论你做什么,都不要将你的节点应用程序放在子目录中Apache知道的,除非您想公开提供这些.js文件。
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.
编辑以回应您的编辑:
如果你的目标是专门转移到Node并最终关闭Apache + PHP,那么你最好的办法就是使用子域名。缺点是你必须在任何地方使用完全合格的链接。好处是当你感觉你的应用程序在节点中时,你可以进行查找/替换(#//(www \。)?newsite.dev#,'// apache.newsite .dev')
和(#// node.newsite.dev#,'// newsite.dev')
,然后当你'完全脱离Apache,只是将其关闭。
EDIT TO RESPOND TO YOUR
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.