What is the significance of utilizing app.set() and app.get() in applications?

Is there a way to simplify this code:

app.set('port', (process.env.PORT || 3000));

const server = app.listen(app.get('port'), () => {
  console.log('Server|Port: ', app.get('port'));
});

Here is an alternative approach that may be easier to understand:

const port = process.env.PORT || 3000;      
const server = app.listen(port, () => {
  console.log('Server|Port: ', port);
});

Answer №1

app.set() and app.get() allow for the storage of custom properties within the app object without interfering with built-in properties. This means you can use them to store any app-level data without worry.

You have the option to store the chosen port using either:

app.set(port, process.env.PORT || 3000) 

and then retrieve it using

app.get('port')

Alternatively, you can go with:

const port = process.env.PORT || 3000;

Both methods are effective; the choice between them is a personal one. Personally, I prefer using const port = xxxx as the port value typically doesn't need to be accessed by other code once the server is running.

Is there a reason for utilizing app.set() and app.get()?

The advantage of app.get() is that any code with access to the app object can easily retrieve the stored property. This makes it convenient for storing app-level data that multiple parts of your codebase may need, even if they are spread across different modules. However, in the case of the port number, it's usually only needed locally, so storing it in a variable works just fine.

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

Instructions for adding and deleting input text boxes on an ASP.NET master page

*I am currently facing an issue with adding and removing input textboxes for a CV form using ASP.NET in a master page. Whenever I click on the icon, it doesn't seem to work as intended. The idea is to have a textbox and an icon "+" to add more textbox ...

Error in Blinking Tooltip when Hovering Skill Bubble (React and d3)

I've encountered a frustrating issue with tooltips on my website - they just won't stop blinking when I hover over the skill bubbles. I tried fixing the tooltips at a certain location, but whenever there's a bubble in that spot and I hover o ...

Escape sequences do not seem to be functioning properly when using innerHTML

I am facing an issue where a string containing HTML escape characters (such as < and >) needs to be rendered inside a div using innerHTML. The intention is for the escaped characters to appear as text rather than as actual HTML, but they still render ...

What could be causing the absence of console.log output in my Netlify function logs?

I am facing an issue with my Netlify function that is scheduled to run every minute using the @netlify/functions package. The function makes API calls and logs the response, but I cannot see any output from the console.log statement in the Netlify function ...

Version 5 of Material UI has a bug where the Grid component does not properly stretch

How can I make the Grid component stretch when one of the Card components contains extra text? You can view the sample code here. Changing the alignItems property to "flex-end" or "center" works, but when using alignItems: "stretch" it does not work. I ...

Navigating with buttons in React JS

In my material-ui, I have a button set up like this: <Button style={green} raised="true" label="Continue to create group"}> CREATE NEW GROUP </Button> I am looking to make it so that when the button is clicked, it will take me ...

Is it possible for a web server to transmit information without being prompted by the client?

I'm looking to create a web application for a tool that can run anywhere from a minute to a couple of hours. I want users to be able to initiate the tool's run directly from the webpage, and receive real-time status updates as the process progres ...

What could be causing the repeated calls to a computed function in vuejs?

In my Vuejs application, I start by fetching initial data and setting it in the store: const app = new Vue({ router, store, mounted: function() { var that = this; $.get("/initial_data/", {}, function(data) { that.$s ...

Exploring the dissimilarity among npm run dev and the parcel index.html

I have been using parcel index.html to set up a local development server, bundling, and hot module replacement. However, I recently learned that npm run dev can do something similar. This has left me wondering: What are the distinctions between the two me ...

JavaScript, keep it easy: globalize

Looking for help with a simple regex expression... document.getElementById('testing').value=f2.innerHTML.replace(">",">\n"); I'm having an issue where it only stops after the first line break. How can I make it work for the enti ...

React JS server conditional response malfunctioning

I've been facing issues with receiving a conditional response from an Express server for a React application. Check out the server-side code below: app.get('/api/checklogin', (req, res) => { var val = req.session.user ? false : tru ...

Storing content from external files in Angular 1.x templateCache

I am currently utilizing the Angular templateCache, following the example set in Angular's documentation. var myApp = angular.module('myApp', []); myApp.run(function($templateCache) { $templateCache.put('templateId.html', ' ...

What are the steps to troubleshoot a running Express.js application in a Docker container that is not accessible?

I created an application utilizing Express.js with Prisma as its ORM. My goal is to deploy the application using Docker, and once it's exposed in the container, I aim to access it locally through docker run -p 3001:3002 klinikpintar/ttv:1.0. In the ...

Generate a fresh array from the existing array and extract various properties to form a child object or sub-array

I am dealing with an array of Responses that contain multiple IDs along with different question answers. Responses = [0:{Id : 1,Name : John, QuestionId :1,Answer :8}, 1:{Id : 1,Name : John, QuestionId :2,Answer :9}, 2:{Id : 1,Name : John, QuestionId :3,An ...

Retrieve the parseJSON method using a string identifier

I am trying to serialize a C# const class into name-value pairs, which I need to access by their string names on the client side. For example: return $.parseJSON(constantClass).property; This approach is not working as expected. Is there any alternative ...

At the ready stance for a particular grade of students attending a class

I have created a page with a navigation menu that can be scrolled using the wheel mouse. My goal is to ensure that the li item with the 'selected' class is always positioned in the first position on the left. Is there a way to achieve this using ...

Obtain a collection of strings from an array of objects based on specified criteria

What is the most efficient method to extract an array of specific strings from an array of objects, where a certain condition needs to be met? Solution Attempt: const array = [{ "Item": "A", "Quantity": 2 ...

Navigating Through the DOM with Selenium using XPath Axes

Currently, I am developing Selenium tests for a dynamic web page. Within this section of code, I am extracting data from an Excel sheet and verifying if a specific element exists on the webpage. I have annotated the critical part of the code with comments ...

Issues with implementing KoGrid within the Durandal JS framework

How do I properly bind a koGrid in my Durandal JS view page? The code provided below is not functioning as expected. View (HTML) <div id="functiontable" class="form-actions"> <div style="height: 200px" data-bind="koGrid: ...

A guide on creating a clickable polygon in Vue.js using Konva

Is there a way to create an interactive polygon in Vue Konva where I can set each corner with a click? I haven't been able to find any resources other than the v-polygon which only creates predefined polygons. ...