问题描述
我在Express/Connect/Jade/Less上有一个Node.js应用是使用Coffescript构建的。
应用程序将部署在几个不同的位置和不同的上下文路径上,例如
http://someurl.com/
http://someotherurl.com/andthenthispath/
我在实现这一点时遇到了问题。我的目的是为上下文路径使用一个变量,并在第二个部署位置使用环境变量填充该变量。
contextPath = process.env.CONTEXT_PATH || ''
然后我可以这样设置我的路线,
app.get contextPath + '/', anIndexFunction
app.get contextPath + '/bla', aBlaFunction
这看起来太乱了,然后我还需要将此变量拉入将构建URL的任何其他位置。
我一直在寻找一种可以更好地处理这种情况的Connect中间件,它存在吗?或者有没有标准的方法来处理这个问题?
推荐答案
您可以使用Express
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}`))