ExpressJS - Environment
Express.js - Modern Development Environment Setup
Section titled “Express.js - Modern Development Environment Setup”Express.js is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. To begin building with Express, you need a modern Node.js development environment. This guide will walk you through setting it up.
Core Development Tools
Section titled “Core Development Tools”Your essential toolkit will include:
- Node.js & npm: The JavaScript runtime and its default package manager.
- A Code Editor: A modern editor like Visual Studio Code (VS Code) is highly recommended for its built-in JavaScript/TypeScript support, integrated terminal, and vast extension ecosystem.
- Version Control: Git is the industry standard for tracking changes and collaborating on code.
- An API Client: Tools like Postman, Insomnia, or the VS Code Thunder Client extension are crucial for testing your API endpoints.
Step 1: Installing Node.js with a Version Manager
Section titled “Step 1: Installing Node.js with a Version Manager”While you can install Node.js directly, using a version manager like nvm (Node Version Manager) is a best practice. It allows you to easily switch between different Node.js versions for different projects.
Installation on macOS / Linux (including WSL for Windows)
Section titled “Installation on macOS / Linux (including WSL for Windows)”Open your terminal and run the official nvm installation script (always check the official nvm repository for the latest command):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bashAfter installation, close and reopen your terminal. Then, install the latest Long-Term Support (LTS) version of Node.js:
# Install the latest LTS versionnvm install --lts
# Set the newly installed version as the defaultnvm use --ltsnvm alias default lts/galliumInstallation on Windows
Section titled “Installation on Windows”For Windows, you can use nvm-windows, a separate but similar tool. Visit the nvm-windows releases page, download the nvm-setup.zip file, and run the installer. Alternatively, embrace the modern workflow by using the Windows Subsystem for Linux (WSL) and following the Linux instructions above.
Verify Your Installation
Section titled “Verify Your Installation”Check that Node.js and npm are installed correctly. You should see version numbers outputted for each.
# Check Node.js versionnode -v# Expected output: v20.11.0 (or your installed LTS version)
# Check npm versionnpm -v# Expected output: 10.2.4 (or a similar version)Step 2: Creating Your First Express Project
Section titled “Step 2: Creating Your First Express Project”Let’s initialize a new project.
- Create a project directory:
mkdir my-express-appcd my-express-app- Initialize a Node.js project: This creates a
package.jsonfile to manage your project’s metadata and dependencies.
npm init -y- Initialize a Git repository: It’s a best practice to start version control from the beginning.
git initecho "node_modules/" > .gitignore- Install Express: Add Express as a project dependency.
npm install express- Install a development dependency:
nodemonis a tool that automatically restarts your server when you make changes to your code, greatly speeding up development.
npm install --save-dev nodemonConfigure npm Scripts
Section titled “Configure npm Scripts”Open your package.json file and add a "start" and "dev" script to the "scripts" section. This provides a standard way to run your application.
{ // ... other properties "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "node src/index.js", "dev": "nodemon src/index.js" }, // ... other properties}Step 3: A Basic “Hello World” Server
Section titled “Step 3: A Basic “Hello World” Server”Create a src directory and a file named index.js inside it.
# In your terminalmkdir srctouch src/index.jsAdd the following code to src/index.js. This code imports Express, creates an app, defines a single route, and starts a server.
import express from 'express';
const app = express();const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => { res.send('Hello, Modern World!');});
app.listen(PORT, () => { console.log(`Server is running at http://localhost:${PORT}`);});Note: To use import/export syntax, add "type": "module" to your package.json file.
{ "name": "my-express-app", "version": "1.0.0", "type": "module", // <-- Add this line // ... rest of the file}Running Your Server
Section titled “Running Your Server”Now, run your development server using the script you created:
npm run devYou should see the message Server is running at http://localhost:3000. Open your web browser and navigate to that URL. You’ll see “Hello, Modern World!” displayed. Your environment is now set up and ready for development!