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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:24:04+00:00 2026-06-11T20:24:04+00:00

In express.js, I would like to provide an additional attribute on the request object

  • 0

In express.js, I would like to provide an additional attribute on the request object for each of my URI listeners. This would provide the protocol, hostname, and port number. For example:

app.get('/users/:id', function(req, res) {
  console.log(req.root); // https://12.34.56.78:1324/
});

I could of course concatenate req.protocol, req.host, and somehow pass around the port number (seems to be missing from the req object) for each one of my URI listeners, but I’d like to be able to do it in a way that all of them could access this information.

Also, the hostname can vary between request (the machine has multiple interfaces) so I can’t just concatenate this string when the application launches.

The goal is to provide URI’s to the consumer which point to further resources in this API.

Is there some sort of way to tell Express that I want req objects to have this additional information? Is there a better way to do this than what I’m outlining?

  • 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-11T20:24:06+00:00Added an answer on June 11, 2026 at 8:24 pm

    You can add a custom middleware that sets the property for each request:

    app.use(function (req, res, next) {
        req.root = req.protocol + '://' + req.get('host') + '/';
        next();
    });
    

    Using req.get to obtain the Host header, which should include the port if it was needed.

    Just be sure to add it before:

    app.use(app.router);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Am working in VC++ 2008 (express) and I would like to write something in
I'm using Visual Studio 2008 Express and I would like Visual Studio (or perhaps
I have a SQL2005 Express database that I would like to create a copy
What is the best way to express range constraint in JPA? I would like
I am using express and I would like to serve a generated image. The
I would like to add to my c# Visual C# Express (no macros) snippet
Here is what I would like to express by Razor: <b>@parameterMapping.Title</b> (Category: @parameterMapping.Category.Title, Regexp:
I'm working with Jade and Express and I would like to use a variable
I would like to distribute my C# application along with SQL Server Express 2005
We are currently using SQL Server 2008 Express Edition, but would like to upgrade

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.