2012-02-24 53 views
3

我在Express/Connect/Jade/Less上使用Coffescript构建了一个Node.js应用程序。在Express/Connect内配置上下文路径

应用程序将在几个不同的地方和不同的上下文路径部署,e.g

  • http://someurl.com/
  • http://someotherurl.com/andthenthispath/

我遇到了实现这一目标的问题。我的目的是为上下文路径使用变量,并在第二个部署位置使用环境变量填充该变量。

contextPath = process.env.CONTEXT_PATH || '' 

然后我就可以建立我的路线,像这样,

app.get contextPath + '/', anIndexFunction 
app.get contextPath + '/bla', aBlaFunction 

这是开始显得过于杂乱,然后我还需要在这个变量来拉这将会建立一个其他任何位置网址。

我一直在寻找一个连接中间件,将以更好的方式处理这种情况,这是否存在?还是有一个标准的方式来处理这个问题?

回答

0

我对Express并不太熟悉,但是如果您确定在出现它时总是希望将它预先加入,那么您不能仅仅在get函数本身中加入CONTEXT_PATH值吗?

+0

我不明白你在说什么。上面使用的get函数(app.get)是配置路由,在这一点上没有任何东西被获取。我目前的解决方案已经在每个匹配器上添加上下文路径。 – 2012-02-26 12:40:50

+0

我的意思是,在应用程序本身中添加CONTEXT_PATH,并修改内置的Express“get”函数来为您预先设置它的值。与此处讨论的内容非常相似:http://stackoverflow.com/questions/4375554/is-it-possible-to-set-a-base-url-for-nodejs-app – 2012-03-02 02:22:08

1

你可以做到这一点与快递

const config = require('./config') 
const argv = require('yargs').argv 
const express = require('express') 
const router = express.Router() 

const app = express() 
router 
    .route('/another-path') 
    .post((req, res) => { 
     // Your code here 
    } 

const contextPath = argv.contextPath || config.contextPath || "/" 

app.use(contextPath, router) 
app.listen(port, host,() => console.log(`Server started on ${host}:${port}${contextPath}`))