Understanding Express

Good morning, I’m having a tough time trying to understand what exactly these lines of code do in Express, could someone look at the comments and fill me in on some details? I’ve looked at about 3 tutorials and I’m still very confused:

const express = require("express");
const app = express();
//A server is created, but where can I access this server? 

app.get('/', function(req, res) {
res.send('Hello World')
})
//METHOD: .get()
//PATH: '/',  what exactly is this? Where am I sending "Hello World"?
//HANDLER: This deals with objects, in this case I'm sending the object 
//(and I'm assuming I will have to format it later on) of "Hello World"

app.listen(5000)
//What is a port?  And if this puts it in a "running state" (what does 
// this mean?), then why not put it at the top of the code?

Thank you for your help, I’m just at the second lesson of Basic Node and Express and I’m not even sure what the goal of the certificate is. Am I creating an API? A server? Accessing/Manipulating an API? I’m generally confused.

Express creates a server for you on “localhost” which you can literally type into your web browser. “localhost” is always the name of the server that runs locally on your own computer. In this case you’ve specified port 5000 though, which would make the full URL this instead: http://localhost:5000

The path “/” refers to the root path on the server, i.e. this is always “/” for any server. It’s the “invisible” slash that most websites have, i.e. the last forward-slash on “https://www.freecodecamp.org/”. While some web browser won’t show that to you, it’s always there and that’s what it refers to.

“res” in res.send() is the response object that Express creates for you. When you’re creating a server, you need to be able to send a “response” back to the client, and this is what that is, defined in a JavaScript object. res.send() is simply a method that you can use to “send” whatever you want back to a client, whether that’s a string “Hello World” in the example, or something else like data that might have been requested.

A port is analogous to a telephone number although it’s not the best one - you can have thousands of ports on a computer, any of which can be used for applications to communicate with each other. The purpose of a port is to provide a specified “address” on an individual computer that can be used to communicate with other applications on that computer, or on other computers located elsewhere.

“listen” is a term that means the server will run the code that you’ve written and then remain available for incoming client requests. Because in some other scenarios, you could hypothetically write code that just ran from the first to last line and exited after doing something. In this scenario however with Express (and other similar back-end frameworks), writing code to run on a server that just exited after running wouldn’t be useful. Computers run code extremely quickly most of the time, and if you didn’t have the app.listen(), this code would run and complete in a fraction of a second, and then exit. Inserting app.listen() forces the app to NOT exit and to “listen” (i.e. monitor) for incoming requests until some sort of failure scenario causes it to stop.

The code you’ve provided here is going to run top-down, so putting app.listen() first will make some things not work. Also consider trying putting it at the top of the code to see what happens - breaking things on purpose will help you learn. :wink:

Right now with this particular lesson you’re just creating a server. Adding more code to it can make it transform into an API if you want to do that.

If you need any further clarification on anything I’ve posted, feel free to ask.