02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Starting up an Express.js server

Starting Up an Express.js Server

In this tutorial, you'll learn how to start up your very first Express.js server. Express.js is a lightweight web framework that sits on top of Node.js and provides a simplified way to create server applications. We will walk through the process of installing Express, setting up a basic server, and creating a Hello World application.

Step 1: Set Up Your Project

The first thing you need to do is set up a new folder for your project. Once you've created the folder, open a terminal window in that directory and initialize a new Node.js project by running the following command:

npm init -y

The npm init -y command will create a package.json file in your project, which will manage the dependencies you'll be using. This step is crucial as it lays the groundwork for adding additional packages, such as Express.

Step 2: Install Express.js

Next, you'll need to install Express.js. In the same terminal, run the following command to add Express to your project:

npm install express --save

This command installs Express locally in your project folder and saves it as a dependency in your package.json file.

Update (2024): With newer versions of Node.js, consider using import statements instead of require if your project is set up to use ES Modules. If you are using type: "module" in your package.json, you can use import express from 'express'; instead of const express = require('express');.

Step 3: Create the Server File

Once you've installed Express, it's time to create your server file. Start by creating a new file named index.js in your project folder. This file will contain the code for your Express server.

Open index.js and add the following code to set up your first Express server:

const express = require('express');
const app = express();
const PORT = 3000;

// Define a simple GET route
app.get('/', (req, res) => {
  res.send('Hello World!');
});

// Start the server
app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
});

In this code:

  • We import Express and create an instance of it by calling express().
  • We define a simple GET route (app.get('/')) that responds with Hello World.
  • Finally, we call app.listen(PORT) to start the server on port 3000 and print a message to the console.

Note: Feel free to change the port number if you have another service using port 3000.

Step 4: Run the Server

To run your server, go back to your terminal and use the following command:

node index.js

If everything is set up correctly, you should see the following message in your terminal:

Server is running on http://localhost:3000

Open your browser and navigate to http://localhost:3000. You should see the message Hello World! displayed on the page. Congratulations, you have successfully created your first Express.js server!

Conclusion

In this tutorial, you learned how to set up a simple Express server, including:

  • Creating a new Node.js project
  • Installing Express.js
  • Creating a Hello World server with Express

This forms the foundation for building more complex applications. In the upcoming tutorials, we will explore how to serve static files, handle routing, and use middleware in Express to extend the capabilities of your server.

Stay tuned as we continue building upon this simple server to create fully functional web applications using Express.js!

Video Transcript

00:00Last section, we dabbled
00:02with creating servers using the native capability of node js
00:06and also using some small quick solutions
00:10that are using modules.
00:12This lecture and this section really is dedicated
00:14to express js.
00:16Express JS is the most popular framework that sits on top
00:20of your native no JS server and exposes
00:24and makes it more simple to interact with it.
00:27So in this section, in this, in this whole chapter,
00:31whole section, we're gonna be covering,
00:32starting up our no js, our our express server,
00:35enabling static files, routing files with express
00:39and routing paths.
00:40With express, we're gonna respond with static files
00:43and understand how to work with moving files around
00:46and how to respond with files when you're running
00:48on an express server.
00:50And we're also gonna learn about how to use middlewares, how
00:53to create middlewares, and how to use the express generator.
00:56So we're gonna cover a lot of things.
00:58By the end of this section,
00:59you're gonna really be comfortable
01:01and familiar with the environment of a node JS server
01:05and express JS more explicitly.
01:07So let's jump right into the first topic.
01:09And the first topic in hand is really
01:11to start up our very first node express JS server.
01:15Uh, kind of a low world type of sample.
01:18And it, and it, we already explained what is express js?
01:21Well, it's a framework that sits on top of node js.
01:25It is not a server because the node js itself,
01:28the technology provides the server capabilities.
01:30Instead of that, it hides the complexities of node js
01:34and exposes an outer layer that is more simple
01:37to interact with.
01:39With that said, express js similarly to node js
01:43assumes dumbness.
01:45In other words, it does not want to tell you how
01:47to run your server, but instead of that, it wants
01:49to expose features
01:50and capabilities to enable other third party solutions,
01:55even on top of it, other frameworks to then have easy access
01:58to control and decide how does it wanna respond
02:01to different server interactions.
02:03For example, express JS as a server does not provide a,
02:07a capability to make sure that users don't have access
02:10to certain areas permission level,
02:13because it assumes that
02:14that is already assuming something about
02:16what we require about the behavior of the site.
02:18And it assumes that you would want
02:20to use a third party solution to sit on top of it
02:23to solve those type of problems.
02:25Another example is that it does not solve the situation
02:28of no pages, uh, error in a page.
02:30It does not assume that if a page
02:32is not there, it is an error.
02:33It assumes that you need to act on it if you want to,
02:36but if you don't, then it's not an error
02:37from its consideration.
02:39Alright, so with that said, let's go ahead
02:40and create a new express JS server
02:43and with it, our hello world to express js.
02:48Alright. All right, let's set up our first express server.
02:52Now you'll notice I'm in a new folder,
02:54move into your new folder.
02:56Um, and let's get started.
02:57So I'm gonna go into terminal Terminal
02:59and what I wanna do first is within this new folder,
03:02I wanna create a new pack.
03:03Oh, excuse me. I wanna set up a new package, a new install.
03:07So I'm gonna call NPM in it
03:09and I'm going to skip
03:10through the dialogue and just create it.
03:12Beautiful. So I have a new,
03:14I have a new, why am I doing that?
03:16I have a new setup. I, I've created my,
03:19my package js ON it's ready.
03:22I have now a pseudo application that's waiting
03:25to start being created.
03:27The next step that I want to do is I want to go ahead
03:29and install express.
03:31And I'm gonna go ahead and save it into our package json
03:37and I'm gonna install express.
03:38It will connect to the web interwebs,
03:41and once it's connected into the interweb,
03:43it will download the package
03:45and install the R Express server just like another package.
03:49And notice how I've done this locally.
03:51So I've basically, this is not a global installation express
03:54was installed locally in the specific,
03:57in the specific folder.
03:59Beautiful. Now I'm gonna go ahead and just touch index js
04:03because that was, that is the file that is expected inside
04:07of our, um,
04:09package json You can see our main file is in index js.
04:13Beautiful. Now that we have that, I'm gonna go ahead
04:15and open that file up
04:17and all the rest of the work is gonna be done
04:19inside of that file.
04:21So the first step that I want to do is I want
04:23to require express, and then I want to instantiate it
04:27and basically create an express application.
04:30So I'm gonna go ahead
04:31and I'm gonna set myself a variable or a constant.
04:35And because this is gonna be a one time
04:37that we create the object, we're never going to replace it.
04:40And, uh, I'm gonna go ahead
04:41and do the shortest, uh, constant that I could do.
04:44And I'm gonna call it app, which is, is, uh, a common name
04:48that is used in the express documentation.
04:51And I'm gonna ask for require express.
04:55And I'm gonna go ahead and once, as soon as I get it,
04:57even without getting a name, I'm gonna go ahead
04:59and run it, basically assigning the value
05:02that comes back from my app right into our constant,
05:06which is called app, which is gonna be our application.
05:09Now that I have my application on my express application set
05:13up and ready, all that's left for me to do is
05:15to define the different paths and to set them up.
05:19So we're gonna create the most simple hello world just
05:21as we've done previously.
05:23And I'm gonna go ahead and I'm gonna use the app get,
05:27and I'm gonna set here
05:28what is the path is the first parameter.
05:30The second parameter is gonna be a callback function
05:33as remember we're talking that we're gonna be creating,
05:35because we're working with no JS at version six plus,
05:39we could already use the latest versions of JavaScript.
05:42And in the latest versions of JavaScript, I could go ahead
05:45and in my jo in my code, literally go ahead
05:48and use my, uh, function arrow functions,
05:52which will basically enable me to put your first,
05:54my first parameters, the request.
05:56The second is the response, and I'm gonna go ahead
06:00and send that arrow open a curly bracket,
06:02close a curly bracket.
06:03And there we go. We have now the response function
06:06that is gonna be called
06:08whenever a match will happen to this, get within this match.
06:12What I want to do is I wanna send the response,
06:14I'm gonna send in a hello world
06:19And I'm gonna, that's it.
06:21I really created the core of my application.
06:23All that's left for me to do is similar,
06:25like we've done it in other HTTP requests
06:28and other applications that we've noticed.
06:30We need to listen to a certain port.
06:34And to do that, I'm gonna call the listen
06:37and I'm gonna set the port to be the thrill.
06:39Let's do 3006 this time.
06:41And again, I'm gonna create your function,
06:44a callback function, an anonymous one using the arrow
06:46notation that is new to ecma script six.
06:50And let me just go ahead here
06:52and I'm just gonna put your console
06:55console log.
07:00We are running on port 3 0 6,
07:07beautiful 3006.
07:08That is all that's left for me to do is
07:11to actually run my applications.
07:12I'm gonna go ahead and save this
07:14and I'm gonna go into my terminal
07:16and literally what I want to do is I wanna call Node
07:18and I'm gonna call index js
07:21and I'm gonna go ahead there and run it.
07:23We're running on port 3006,
07:25and if I go back into my browser, I'm gonna go directly
07:28to my local host and I'm gonna assign it the 3006 port.
07:33And if I go ahead, I'm gonna see hello world.
07:35I've just created the most simple
07:37Hello World Express.
07:41Okay, so we started off easy
07:43and in the next lecture we're gonna build upon the simple
07:46structure that we've created and see
07:48how could we continue building out our
07:50application, our server.
07:53In this lecture we created our hello World
07:56express JS server.
07:59With that said, in the next lecture,
08:00we're gonna take it a step further from just saying hello
08:03to the world where we'll take a look at
08:04how do you enable static files using Express js.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!