At what point is the JavaScript function expression triggered in this code snippet?

let express = require('express')

let app = express();

app.use(express.static('static'));

let server = app.listen(3000, function() {
    let port = server.address().port;
    console.log("The server has started on port", port);
});

I am new to JavaScript. In this code snippet, the server() function is never explicitly called, yet it still executes. It is not an immediately invoked function expression (IIFE). I'm curious about when exactly the app.listen() method gets executed.

Original code sourced from: https://github.com/vasansr/react-tutorial-mern/blob/master/webapp.js

Answer №1

As soon as the line is reached, app.listen gets executed.

The server now listens on port 3000: var server = app.listen(3000, function() { ...

Port 3000 is now open for incoming connections.

The callback function in the second parameter of listen will trigger when a connection is made to port 3000.

Answer №2

According to the information found in the documentation of expressjs:

The method app.listen() is designed to return an http.Server object and serves as a shortcut for the following code:

app.listen = function() {
  var server = http.createServer(this);
  return server.listen.apply(server, arguments);
};

Answer №3

The server isn't a function, it's actually a variable. The code snippet app.listen(...) is being run directly on line 7. The outcome of this action is then stored in the variable server.

The app.listen() function contains a callback method as its second parameter. This callback gets triggered once the app begins listening for requests. At that point, calling server.address().port will provide you with the server's port number.

Answer №4

It appears that the server is not being utilized in any part of the code. It's worth noting that the server may be an object returned from the app.listen function rather than a traditional function.

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

How can I properly integrate multer with Node and Express in this situation?

I've been working on setting up a route for uploading photos, but after making some changes, it has stopped functioning and I'm not sure how to fix it. const multer = require('multer'); // MULTER STORAGE const multerStorage = multer.di ...

Persistently save retrieved information and store data in MongoDB by utilizing Node.js

I am facing the challenge of continuously making an http.get request to an API that provides location data. I have tried a basic get request to test if the data is being received, and it is. However, the issue is that I need this process to run in a contin ...

Tips for displaying previous values when discarding changes to a record in a material-ui table

How can I prevent changes from reflecting in a material-ui table when clicking on the X icon while editing a row? Is there a way to only save the edited record on the check (_) icon instead? Any suggestions or solutions would be greatly appreciated as I am ...

Instead of scrolling through the entire window, focus on scrolling within a specific HTML element

I currently have the following elements: #elementA { position: absolute; width: 200%; height: 100%; background: linear-gradient(to right, rgba(100,0,0,0.3), rgba(0,0,250,0.3)); z-index: 250; } #containerofA { position: fixed; ...

Utilizing the mobile navigation layout for desktop screens without the need to adjust breakpoints

Having created a responsive site with common breakpoints established in SCSS as mixins, I am now seeking to implement the mobile breakpoint for a single page on desktop view only. Initially, my plan was to create a 'forced-mobile' class by exten ...

Direct the /username path to /[user]/[tab].js instead of [user]/index.js in Next.js

My goal is to develop a user profile page that displays content based on the current tab the user is viewing. The tab is determined by what is provided in the URL (e.g. /username/tab1). The challenge I am facing is that one of the tabs the user can access ...

Splitting the div into two columns

I've encountered various solutions to this issue, but when I integrate an Angular2 component inside the divs, it fails to function properly. Here is my progress so far: https://i.stack.imgur.com/qJ8a9.jpg Code: <div id="container"> <div ...

The background image refuses to load

I've been working on a website project using nodejs, expressjs, and pug as my template engine. I designed the home page in pure pug but ran into an issue when trying to add a background image to a specific section of the site. Despite double-checking ...

Techniques for retrieving user inputs upon loading the HTML document and detecting changes using JavaScript

I am new to using JS to pass data to Python. I have managed to collect the values when the HTML is loaded, but I am facing an issue where the value does not change when it is updated. Here is the code snippet that I have been working with: <input type= ...

Testing the screen size automatically using Javascript

Hello everyone, I've implemented a JavaScript code that triggers an image to slowly appear and darken the background when a link is clicked. However, on an iPad, the background doesn't completely turn black as intended. The CSS specifies that the ...

Utilizing Gitlab Runner to store node_modules as an artifact rather than in the cache

Based on the GitLab Documentation comparison between cache and artifacts, it is recommended to store node_modules as cache instead of an artifact. The challenge arises when multiple runners are involved, as a different runner may pick up a job created by a ...

Is the input in the array? How to find out?

I am currently facing an issue with a script that I have created to count array elements matching a user input. Strangely, all if statements in the script seem to be returning false. I have verified that both the array element and the input value are stri ...

Executing a Shortcode Using a Button in Visual Composer for Wordpress

It should be easy to do this. I've got a great plugin with a modal newsletter signup form that offers various launch options, including manual launching with the following codes. https://i.stack.imgur.com/IGbsp.png My theme utilizes Visual Composer. ...

Steps to generate a fresh array from a given array of objects in JavaScript

Looking to transform an existing array into a new array of objects in Vue.js. Here's the initial array : import { ref } from 'vue'; const base_array = ref([ {id: 1, name: Pill, timing: [morning, noon, evening, night]}, {id: 2, name: Ta ...

The package "XXX" could not be located within the "npm" registry

Having some trouble with my yarn installation. While the package installs correctly using npm install, I encounter an error when attempting to use yarn: Error: Couldn't find package "google-map-react" on the "npm" registry. .npmrc Configuration @co ...

Effortless creation of REST APIs for performing CRUD operations on MongoDB

I manage a mongodb database with over 50 collections. My goal is to automatically generate CRUD REST APIs for these collections. What is the most efficient approach? Preferably using Node/Express framework Utilizing Java on the backend Currently, I am u ...

Changing a file object into an image object in AngularJS

Can a file object be converted to an image object? I am in need of the width and height from the file (which is an image). Here is my code: view: <button id="image_file" class="md-button md-raised md-primary" type="file" ngf-select="uploadFile($file ...

React's conditional statement is malfunctioning

I am attempting to display a specific form for review if the user meets the following criteria: 1. Logged in 2. Not an administrator 3. Not a moderator My code currently looks like this: <h2>Write a Review</h2> {errorResortReview &a ...

Creating curved triangles in React Native is a fun and easy way to add stylish

Hello everyone, I am a newcomer to react native and I am attempting to create the following user interface. Is there any way to create a curved triangle? I have tried but I am unable to curve the edges of the triangle. https://i.stack.imgur.com/vE17U.png ...

The lambda function is encountering a MySQL error stating that it cannot find the module 'mysql' and is not functioning properly, even after being included in the zipped file

I set up a lambda function using the following steps. First, I created a new folder and initialized a new project by running npm init in the command line Next, I added my code to index.js and installed the mysql package locally by running npm insta ...