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 9110971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T03:23:42+00:00 2026-06-17T03:23:42+00:00

I have a few static pages served with Express.JS. The setup is easy: var

  • 0

I have a few static pages served with Express.JS. The setup is easy:

var app = express();

app.configure(function(){
  app.use(express.static(path.join(application_root, "StaticPages")));
  app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

I want the response to include an addittional http header (Access-Control-Allow-Origin:*). Where should it be placed?
I tried the below sample, but of course the header appears only on the default page:

app.get('/', function(req, res){
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.send('Hello World');
});

Thanks.

  • 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-17T03:23:43+00:00Added an answer on June 17, 2026 at 3:23 am

    I tried the below sample, but of course the header appears only on the default page

    Yes, that is because you defined it just for the GET / route and not for the other paths. You should use a middleware instead.

    If you wish to set the header for all requests:

    app.configure(function(){
      app.use(function(req, res, next) {
        res.setHeader("Access-Control-Allow-Origin", "*");
        return next();
      });
      app.use(express.static(path.join(application_root, "StaticPages")));
      app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    });
    

    If you just want to do it for the static folders, there is no general method. You can probably change the express.static(which comes from connect.static). Another way to do it is to match urls and set the header if the url is matched.

    app.configure(function(){
      app.use(function(req, res, next) {
        var matchUrl = '/StaticFolder';
        if(req.url.substring(0, matchUrl.length) === matchUrl) {
          res.setHeader("Access-Control-Allow-Origin", "*");
        }
        return next();
      });
      app.use(express.static(path.join(application_root, "StaticPages")));
      app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
    });
    

    NOTE: that the middleware need to be before the routes to make effect, in other words you can’t put the middleware after the static middleware.

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

Sidebar

Related Questions

I have a few static pages that are just pure HTML, that we display
I have upgraded my grails app from 1.3.7 to 2.0.1. I had few static
I have a simple solution in Visual Studio 2010 containing a few static libraries,
I have written a mini web-server which needs to serve up a few static
I have a few classes as shown here public class TrueFalseQuestion implements Question{ static{
I have some TableViewController with static Cells and some TextFields. A few Days ago,
i have few confusion regarding ORM(Object Relational Mapping) i am thinking about to use
I've an existing project in symfony 1.4 For few new static pages, I added
I have been learning C# for the past few days for use with ASP.NET
I want build a lightweight cached system (few server-resources) for easy serve pages (without

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.