是否可以设置NodeJS应用程序的基本URL? [英] Is it possible to set a base URL for NodeJS app?

查看:102
本文介绍了是否可以设置NodeJS应用程序的基本URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要在同一个域下托管多个NodeJS应用程序,而不使用子域(如google.com/reader而不是images.google.com)。问题是我总是打字网址的第一部分,例如Express / NodeJS中的/ reader。



如何设置Express应用程序,使基址为 something.com/myapp



所以而不是:

  app.get(/ myapp,function(req,res){
//可以从something.com/myapp
}访问);

我可以做:

  //某些设置
app.base =/ myapp

app.get(/,function(req,res){
//仍然可以从something.com/myapp
}访问);

我还要配置Connect的staticProvider的行为方式相同(现在默认为服务静态文件到 something.com/js something.com/css 而不是的东西。 com / myapp / js

解决方案

目前还不支持,这不容易添加它自己。



整个路由的东西埋在服务器代码的深处,作为奖金,没有暴露他们自己的路由。 >

我挖掘了源代码,并检出了最新版本的Express和Connect中间件,但是仍然不支持此类功能,您应该在 Connect Express 本身。



同时...



自己修补这个东西,这里是一个简单而简单的方法,只需修改一行代码。



〜/ .local / lib / node / .npm / express / 1.0.0 / package / lib / express / servers.js ,搜索:

  //生成路由
this.routes [method](path,fn);

这应该在 357 之间,替换与:

  //生成路由
this.routes [method](((self.settings.base ||'')+ path),fn);

现在只需添加设置:

  app.set('base','/ myapp'); 

对于简单字符串的路径,这可以正常工作,对于RegEx支持,您将不得不在路由器中间件自己,更好的解决了这个问题。



就静态提供者而言,只需添加 / mypapp

更新



RegExp:

  //替换
this.routes [method](baseRoute(self.settings.base || '',path),fn);

// helper
function baseRoute(base,path){
if(path instanceof RegExp){
var exp = RegExp(path).toString()。切片(1,-1);
return new RegExp(exp [0] ==='^'?'^'+ base + exp.substring(1):base + exp);

} else {
return(base ||'')+ path;
}
}

我只用几个表达式测试了,所以这不是100%的测试,但在理论上应该是有效的。



更新2



提出补丁的问题:

https:// github.com/visionmedia/express/issues/issue/478


I want to be able to host multiple NodeJS apps under the same domain, without using sub-domains (like google.com/reader instead of images.google.com). The problem is that I'm always typing the first part of the url e.g. "/reader" in Express/NodeJS.

How can I set up an Express app so that the base URL is something.com/myapp?

So instead of:

app.get("/myapp", function (req, res) {
   // can be accessed from something.com/myapp
});

I can do:

// Some set-up
app.base = "/myapp"

app.get("/", function (req, res) {
   // can still be accessed from something.com/myapp
});

I'd also like to configure Connect's staticProvider to behave the same way (right now it defaults to serving static files to something.com/js or something.com/css instead of something.com/myapp/js)

解决方案

At the moment this is not supported, and it's not easy to add it on your own.

The whole routing stuff is buried deep inside the server code, and as a bonus there's no exposure of the routes them selfs.

I dug through the source and also checked out the latest version of Express and the Connect middleware, but there's still no support for such functionality, you should open a issue either on Connect or Express itself.

Meanwhile...

Patch the thing yourself, here's a quick and easy way with only one line of code changed.

In ~/.local/lib/node/.npm/express/1.0.0/package/lib/express/servers.js, search for:

// Generate the route
this.routes[method](path, fn);

This should be around line 357, replace that with:

// Generate the route
this.routes[method](((self.settings.base || '') + path), fn);

Now just add the setting:

app.set('base', '/myapp');

This works fine with paths that are plain strings, for RegEx support you will have to hack around in the router middleware yourself, better file an issue in that case.

As far as the static provider goes, just add in /mypapp when setting it up.

Update

Made it work with RegExp too:

// replace
this.routes[method](baseRoute(self.settings.base || '', path), fn);

// helper
function baseRoute(base, path) {
    if (path instanceof RegExp) {
        var exp = RegExp(path).toString().slice(1, -1);
        return new RegExp(exp[0] === '^' ? '^' + base + exp.substring(1) : base + exp);

    } else {
        return (base || '') + path;
    }
}

I only tested this with a handful of expressions, so this isn't 100% tested but in theory it should work.

Update 2

Filed an issue with the patch:
https://github.com/visionmedia/express/issues/issue/478

这篇关于是否可以设置NodeJS应用程序的基本URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆