我有一个在 Expressconnect/Jade/Less 上使用 Coffeescript 构建的 Node.js 应用程序。
该应用程序将部署在几个不同的地方和不同的上下文路径上,例如
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}`))
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)