Utilize the underscore symbol when iterating through an Express application

Here is a snippet of my CoffeeScript code:

  dirs = ["/assets", "/public", "/locales", "/data/topo"]
  app.configure ->
      app.use assets(build : true)
      jsPaths assets, console.log
      @use(express.favicon(process.cwd() + "/assets/images/favicon.ico", {maxAge:maxAges}))
      .use(express.compress())
      .use(express.static(process.cwd() + "/assets", {maxAge:maxAges}))
      .use(express.static(process.cwd() + "/public", {maxAge:maxAges}))
      .use(express.static(process.cwd() + "/locales", {maxAge:maxAges}))
      .use(express.static(process.cwd() + "/data/topo", {maxAge:maxAges}))
      .use(express.logger('dev'))
      .use(express.errorHandler(
            dumpException: true
            showStack: true
      ))
  #  Add template engine

I want to set the maxAge for all directories listed in

dirs = ["/assets", "/public", "/locales", "/data/topo"]
. Can anyone provide guidance on the correct way to achieve this?

Any advice would be greatly appreciated.

Answer №1

Executing a method for each item in an array?

Apply maxAge setting to folders: 
setMaxAge(folder) ->
  app.use express.static( process.cwd() + folder, maxAge:maxAges )

Alternatively, in a single line:

app.use express.static(process.cwd() + folder, maxAge:maxAges) for folder in ["/assets", "/public", "/locales", "/data/topo"]

Refer to the coffeescript documentation for loops

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Track changes and save only the updated elements in an array

Currently, I am utilizing Angular for the front end, C# for the backend, and an Oracle database for a project with a company. Within the grids provided to me, there are more than 120 records that users can edit individually. My dilemma lies in identifyin ...

When using an HTML dropdown select to display a list of generated users, only the value of the first user

I have a template that fetches a list of users from the database, and each user row contains a dropdown select option to change their user type (this is an admin page). My goal is to make the dropdowns submit the changes asynchronously. Currently, I can on ...

How to use Python and JavaScript to make a WebElement visible in Selenium

I am currently facing an issue with uploading a PNG file using Selenium. The input element necessary for the upload process is visible to the user but not to Selenium. Following the suggestions in the Selenium FAQ, I tried using JavaScriptExecutor as shown ...

What is the best way to have NextJS add styles to the head in a production environment?

Typically, NextJS will include <style> tags directly in the head of your HTML document during development (potentially utilizing the style-loader internally). However, when running in production mode, NextJS will extract CSS chunks and serve them as ...

Bugfender is reporting a ReferenceError stating that it can't locate the variable BroadcastChannel

While Bugfender works well in my Vue.js application on Chrome, I am experiencing issues with it on my Safari Mac. Specifically, I am encountering an error in the browser console that says "ReferenceError: Can't find variable: BroadcastChannel". This i ...

Attempting to employ the .reduce method on an object

Currently, I am faced with the task of summing a nested value for all objects within an object. The structure of my object is as follows: const json = [ { "other_sum": "1", "summary": { "calculations" ...

Iterate through each row of the PHP array

Hey there, I have two sets of code that I'd like to share with you. The first one is an Ajax snippet: /* Loop and Retrieve Data from Database to Generate a Table */ $(document).ready(function () { $('#btngenerate').click(function(e){ ...

What is the best way to manage the back button functionality on pages that use templates?

I am currently developing a website using angularjs. The layout consists of two main sections: the menu and the content area. For instance This is an example page: /mainpage <div> <div id="menu"> <div ng-click="setTemplate('fi ...

The total number of items in the cart is experiencing an issue with updating

For a recording of the issue, click here: While everything works fine locally, once deployed to production (vercel), it stops working. I've tried numerous approaches such as creating a separate state in the cart, using useEffect with totalQuantity in ...

Nextjs is facing challenges in enhancing LCP specifically for text content

I've been trying to boost my LCP score by optimizing the text on my page, but it seems stuck and I can't figure out why my LCP isn't improving. Take a look at the screenshot: https://i.stack.imgur.com/xfAeL.png The report indicates that &a ...

Is it accurate to categorize every ajax request (using xmlhttprequest) as a web service request?

Recently, I began incorporating AngularJS with Spring MVC as my backend. I have been utilizing $resource to connect with my backend. Given that this is a restful service and $resource operates using ajax, I find myself questioning: 1) Is ajax solely used ...

Using nodeJs and Express to retrieve a response and then invoking another function

I have a post.route.js file where I specify : var post = require('../controllers/post.controller'); router.route('/posts').get(post.getPosts, post.setCache); and my post.controller.js contains : exports.getPosts = function(req, res, ...

Storing Form Images in MongoDB with Multer in NodeJS and Sending as Email Attachment

I have been working on a website that allows users to input details and attach images or PDF files (each less than 5MB) to the form. My goal was to store the entire image in my MongoDB database and also send it to my email using Nodemailer in Node.js. Whi ...

How to retrieve the content of <p> elements by their id or class in PHP

I am currently utilizing a Script that enables the display of URL content within the meta tag "description". This script utilizes PHP in the following manner: $tags = get_meta_tags($url); Subsequently, it is called like so: <label class="desc"> ...

Ways to compare two arrays based on a specific field using JavaScript

I have two arrays, known as array 'a' and array 'b'. var a = [ [1,'jake','abc',0 ], ['r', 'jenny','dbf',0] , ['r', 'white','dbf',0] ] var b = [ ['s&ap ...

Error: Unable to iterate over JSON data as json.forEach is not a valid function

let schoolData = { "name": "naam", "schools" : [ "silver stone" , "woodlands stone" , "patthar" ], "class" : 12 } schoolJSON = JSON.stringify(sc ...

Converting objects to arrays in AngularJS and Ionic: A simple guide

In the scenario where I have an object structured like this: {first: "asdasd", second: "asdas", third: "dasdas", four: "sdasa"}, my objective is to convert this object into an array. if(values){ var first=values.first; var second=values.second; var ...

When the nodejs server is started, the worker_threads do not execute

I've been diving into the world of nodejs worker_threads. I'm trying to terminate a worker when the server starts, just like this: #!/usr/bin/env node const bole = require('bole'); const { Worker } = require('worker_threads&ap ...

The object labeled as <Resource> does not contain the function 'push'

Initially, I encountered this issue with the code Expected response to contain an object but got an array: angular.module('homeModule').factory("TodosFactory", ['$resource', function ($resource) { return $resource('/api/todos/:tod ...

What is the mechanism by which a Node.js server handles incoming requests?

Suppose I am working with this code snippet. I am using ExpressJS, although the server part doesn't seem much different from vanilla Node.js. var express=require('express'); var settings=JSON.parse(fs.readFileSync('settings.json' ...