Getting Started with Express: How to Set Up Your First Server

Beginner's Step-by-Step Guide to Setting Up Your First Express Server

Table of contents

No heading

No headings in the article.

So, you've made the exciting choice to start your web development journey with Express.js as your server framework. Great decision! Express.js is a fast, unopinionated, and minimalist web framework for Node.js that offers a strong set of features for web and mobile apps. In this tutorial, we'll guide you through the fundamentals of building a server with Express.

Step 1: Setting Up Your Environment

Before we start coding, ensure that you have Node.js installed on your computer. You can get it from here. After installing Node.js, open your terminal or command prompt and make a new folder for your project.

mkdir my-express-server
cd my-express-server

Now, let's initialize a new Node.js project using npm (Node Package Manager):

npm init -y

This command will create a package.json file in your project directory, which will help manage your project dependencies.

Step 2: Installing Express

Next, let's add Express as a necessary part of our project. In your terminal, type the following command:

npm install express

This command will download and install Express in your project directory.

Step 3: Creating Your Server

Now that Express is installed, it's time to make a basic server. Make a file called server.js in your project folder and open it in your preferred code editor. Copy and paste the following code into server.js:

const express = require('express');

// Create an instance of Express
const app = express();

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

// Start the server
const port = process.env.PORT || 4200;
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

Step 4: Starting Your Server

To start your server, simply run the following command in your terminal:

node server.js

This will start your server on port 4200 by default. You should see the message "Server is running on port 4200" logged to the console.

Step 5: Testing Your Server

Open your web browser and navigate to http://localhost:4200. You should see the message "Hello, World!" displayed in your browser.

Congratulations! You have successfully built your first server using Express. Now, you can dive deeper into Express and develop more advanced applications.

Conclusion

In this guide, we learned how to create a server using Express.js. We set up our environment, installed Express, made a basic server, started it, and tested it in the browser. Express.js is great for building web apps, and we've just begun exploring its capabilities.

Feedback

I hope this guide has been helpful in getting you started with Express.js. If you have any suggestions, tips, or feedback, please feel free to leave a comment below. Your input is valuable and can help improve future guides. Don't forget to follow me for more insightful content on Express.js and other web development topics. Happy coding!

Did you find this article valuable?

Support Sumit Singh's blog by becoming a sponsor. Any amount is appreciated!