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 ofrequire
if your project is set up to use ES Modules. If you are usingtype: "module"
in your package.json, you can useimport express from 'express';
instead ofconst 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!