Node.js and Express.js Essentials — A Quickstart Guide for Beginners
Introduction
Node.js has become a popular choice for developers when building scalable and efficient web applications. Express.js is a powerful web framework for Node.js that simplifies server-side development. In this article, we’ll walk through the basics of getting started with Node.js and Express.js, including setting up a development environment, creating a simple web server, and building a basic web application.
Prerequisites
Before diving into Node.js and Express.js, ensure you have the following prerequisites:
- A basic understanding of JavaScript and Node.js.
- Node.js installed on your system. Download the latest version from the Official Website.
- A code editor of your choice, such as Visual Studio Code, Sublime Text, or Atom.
Setting up Your Development Environment
To start using Node.js and Express.js, you need to create a new project directory and initialize a Node.js application:
- Create a new directory for your project and navigate to it using the terminal or command prompt.
- Run the command
npm init
to initialize a new Node.js application. Follow the prompts to set up yourpackage.json
file. - Install Express.js by running the following command:
npm install express --save
. This will add Express.js as a dependency in yourpackage.json
file.
Creating a Simple Web Server with Express.js
Now that your development environment is set up, let’s create a simple web server using Express.js:
- Create a new file called
app.js
in your project directory. - Open
app.js
and import the Express module:
const express = require('express');
- Create a new Express application:
const app = express();
- Define a route for the root URL (
/
) that returns a "Hello, World!" message:
app.get('/', (req, res) => {
res.send('Hello, World!');
});
- Set your application to listen on a specific port:
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
Now, start your application by running the command node app.js
in your terminal. Your server should now be running on port 3000. Navigate to http://localhost:3000
in your web browser, and you should see the "Hello, World!" message.
Building a Basic Web Application with Node.js and Express.js
With your simple web server up and running, let’s expand your application by adding more routes and serving static files:
- Create a new directory called
public
in your project folder. - Inside the
public
folder, create an HTML file calledindex.html
with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Node.js and Express.js App</title>
</head>
<body>
<h1>Welcome to our Node.js and Express.js App!</h1>
<p>This is a simple web application built with Node.js and Express.js.</p>
</body>
</html>
- Configure your Express application to serve static files from the
public
directory:
app.use(express.static('public'));
- Add a new route for the
/about
page:
app.get('/about', (req, res) => {
res.send('<h1>About Us</h1><p>We are a team of developers passionate about Node.js and Express.js.</p>');
});
Now, restart your application by stopping the previous node app.js
command (Ctrl + C) and running it again. Navigate to http://localhost:3000
in your web browser to see the contents of index.html
. You can also visit http://localhost:3000/about
to view the About Us page.
Final app.js Code:
const express = require('express');
const app = express();
// Define a route for the root URL
app.get('/', (req, res) => {
res.send('Hello, World!');
});
// Set the application to listen on a specific port
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
// Serve static files from the public directory
app.use(express.static('public'));
// Add a new route for the /about page
app.get('/about', (req, res) => {
res.send('<h1>About Us</h1><p>We are a team of developers passionate about Node.js and Express.js.</p>');
});
Conclusion
In this article, we introduced the basics of getting started with Node.js and Express.js. We showed you how to set up a development environment, create a simple web server, and build a basic web application with multiple routes and static file serving. With this foundation, you can continue to explore the powerful features of Express.js and create more complex web applications with ease.