Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8106349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:26:21+00:00 2026-06-06T00:26:21+00:00

I just inherited a project with node.js/express written in coffeescript and using jade to

  • 0

I just inherited a project with node.js/express written in coffeescript and using jade to render views. In my views folder I have about 20 jade files that need to have routes set up. Rather than repeat myself over and over like so (which is currently working just fine):

app.get '/html/404.html', (req,res) ->
  res.render '404',

app.get '/html/503.html', (req,res) ->
  res.render '503', 

app.get '/html/foo.html', (req,res) ->
  res.render 'foo', 

I’d prefer to keep this less messy by creating an array of the urls and using a for loop to iterate over it, something like this:

urls = [
  "404"
  "503"
  "foo"
]

for url in urls
  app.get "/html/#{url}.html", (req,res) ->
    res.render "#{url}",

Unfortunately this isn’t working. Any advice?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:26:22+00:00Added an answer on June 6, 2026 at 12:26 am

    This is a classic problem that happens when you define a closure in a loop in JavaScript: You expect the value of url in your route function to be whatever it was when you defined that function, but in fact, it’s whatever the last value in the loop was. There’s only one url variable, after all.

    The fix is to “capture” that variable in an outer closure, which is why CoffeeScript has the do syntax:

    for url in urls
      do (url) ->
        app.get "/html/#{url}.html", (req,res) ->
          res.render "#{url}"
    

    For more on this, see my PragPub article A CoffeeScript Intervention.

    (Having said this, Arnaud’s suggestion of using an Express param in the route is a much more elegant solution. If you can avoid a loop entirely, do so!)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've just inherited a project, and been told that an entire folder, includes/ needs
I've just inherited a large project previously coded by about 4-5 people. The documentation
I just inherited a project with LinQ to SQL Database file. I need about
I have just been assigned an inherited coldfusion project. The developer dropped everything and
I just inherited this TYPO3 project and have a bunch of tasks to perform
I've inherited a project and we are using git. We have a number of
I just inherited a large web project using SVN for VCS. There are no
I have the following code in the VB6 project I just inherited. Case Is
I've just inherited a project which was maintained using Git. At one point, the
I just inherited a PHP project that generates large PDF files and usually chokes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.