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!

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!

Starting up an Express.js server

Learn how to start up an Express.js server, including installation, basic setup, and creating a Hello World server.

08:08

Enabling static files

Learn how to enable static files in Express.js to serve HTML, CSS, JavaScript, and other assets in your web application.

04:47

Routing with Express

In this video, explore the basics of routing in Express.js and how to create dynamic content. Learn about GET, POST, PUT, DELETE, and how to handle different routes effectively.

09:08

Routing with Forms and Static Files in Express.js

Learn how to create dynamic routes with forms and static files using Express.js. This is essential for building interactive web applications.

06:14

Using Middleware in Express.js

Learn how to integrate third-party middleware in Express.js, specifically using body-parser, to handle form data and extend server functionality.

07:29

Creating a middleware in ExpressJS

Learn how to create your own middleware in ExpressJS to handle custom server-side logic and responses.

03:54

Using Express Generator

Learn how to quickly scaffold Express.js applications using the Express Generator tool. This tool helps automate server setup and structure, making development faster and easier.

04:34

Back to React

Learn about integrating Webpack and Babel with React to streamline full stack development.

18:16