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

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

问题描述

我希望能够在同一域下托管多个 NodeJS 应用程序,而不使用子域(例如 google.com/reader 而不是 images.google.com).问题是我总是输入网址的第一部分,例如Express/NodeJS 中的/reader".

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.

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

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

所以代替:

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

我能做到:

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

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

我还想将 Connect 的 staticProvider 配置为以相同的方式运行(现在它默认将静态文件提供给 something.com/jssomething.com/css 而不是 something.com/myapp/js)

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.

我翻阅了源代码并检查了最新版本的 Express 和 Connect 中间件,但仍然不支持此类功能,您应该在 ConnectExpress 本身.

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.

同时...

自己修补,这是一种快速简便的方法,只需更改一行代码.

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

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

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

这应该在 357 行附近,替换为:

This should be around line 357, replace that with:

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

现在只需添加设置:

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

这适用于纯字符串的路径,对于 RegEx 支持,您必须自己在路由器中间件中进行修改,在这种情况下最好提交问题.

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.

就静态提供者而言,只需在设置时添加/mypapp即可.

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

更新

也让它与 RegExp 一起使用:

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;
    }
}

我只用少数表达式测试了这个,所以这不是 100% 测试,但理论上它应该可以工作.

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

更新 2

提出补丁问题:
https://github.com/visionmedia/express/issues/issue/478

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

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