4

I have a Node.js app built using Coffescript on Express/Connect/Jade/Less.

The app will be deployed in a couple of different places and on different context paths, e.g

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

I'm experiencing problems achieving this. My intention was to use a variable for the context path and populate that with an evironment variable in the second deployment location.

contextPath = process.env.CONTEXT_PATH || ''

I can then set up my routes like so,

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

This is starting to look overly messy, and then I also need to pull in this variable in any other location which will build a url.

I've been looking for a piece of Connect middleware that will handle this scenario in a nicer way, does that exist? Or is there a standard way to handle this?

aynber
  • 22,380
  • 8
  • 50
  • 63
Dan Midwood
  • 18,694
  • 7
  • 33
  • 32

3 Answers3

4

You can do that with 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}`))
Skeeve
  • 1,205
  • 3
  • 16
  • 31
0

Suppose that you have the following application, and you want to give a context path to all routes registered on the below app object:

const express = require('express')
const app = express()

app.get('/hello', (req, res) => {
  res.send('Hello World!')
})

app.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000`)
})

You can turn the original app object into an express sub-app, and mount it on a context path! This works just like an express router, but without having to change all existing express app get/post routings.

const express = require('express')
const app = express()

app.get('/hello', (req, res) => {
  res.send('Hello World!')
})

const root = express();
const contextPath='/v2';  // you can get this from env too.
root.use(contextPath, app)
root.listen(3000, () => {
  console.log(`Example app listening at http://localhost:3000`)
})

BTW, in addtion to give your app a context path right in in the app, you can also use other tools like nginx.

Iceberg
  • 2,744
  • 19
  • 19
0

I'm not overly familiar with express, but couldn't you just prepend the CONTEXT_PATH value within the get function itself, if you're sure you always want to prepend it when it's present?

Rylab
  • 1,236
  • 7
  • 17
  • I'm not sure I understand what you're saying. The get function I used above (app.get) is configuring the routing, nothing is being getted at that point. My current solution is already prepending each matcher with the context path. – Dan Midwood Feb 26 '12 at 12:40
  • I meant, add the CONTEXT_PATH within the App itself, and modify the built-in Express "get" function to prepend its value for you. Very similar to what was discussed here: http://stackoverflow.com/questions/4375554/is-it-possible-to-set-a-base-url-for-nodejs-app – Rylab Mar 02 '12 at 02:22