Setup Node.js Application 🛠️
In this section, you'll learn how to set up a Node.js application from scratch, using the following tools: VS Code, Node.js, and Docker. Let's get started!
📋 Outline
- [ ] What Are the Starter Steps?
- Install VS Code
- Install Node.js
- Install Docker
- Create a Simple Node.js Application
- Dockerize the Application
🟢 What Are the Starter Steps?
Before diving into Node.js development, you'll need to set up a few essential tools on your machine.
1. Install Visual Studio Code (VS Code)
VS Code is a lightweight, powerful code editor that's great for writing and debugging Node.js code.
Steps to Install:
- Go to the official VS Code website: VS Code Download.
- Download the installer for your operating system (Windows, macOS, or Linux).
- Follow the installation prompts.
Bonus: Install the Node.js Extension Pack in VS Code for additional Node.js development features.
2. Install Node.js
Node.js is the runtime that allows you to execute JavaScript on the server.
Steps to Install:
- Visit the Node.js official website: Node.js Download.
- Download the latest LTS (Long-Term Support) version for your platform.
- Follow the instructions to install Node.js and npm (Node Package Manager), which is automatically bundled with Node.js.
Verify Installation:
- Open a terminal and run the following commands to confirm that Node.js and npm are installed:
You should see the versions of Node.js and npm printed out.
3. Install Docker
Docker helps containerize your application, ensuring it can run consistently in different environments.
Steps to Install:
- Go to the official Docker website: Docker Download.
- Download Docker Desktop for your operating system.
- Follow the installation instructions provided for your platform.
- Once installed, start Docker Desktop.
Verify Installation:
- Run the following command in your terminal to check if Docker is installed:
You should see the Docker version printed out.
🚀 Create a Simple Node.js Application
Now that we have the required tools, let's create a simple Node.js application.
1. Initialize a New Node.js Project
- Create a new directory for your project:
- Initialize a Node.js project by running:
This will create a package.json
file that stores metadata about your project and its dependencies.
2. Write a Simple Server
Create an index.js
file in your project directory and add the following code:
const http = require("http");
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader("Content-Type", "text/plain");
res.end("Hello, World!\n");
});
const PORT = 3000;
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}/`);
});
3. Run the Application
To start your Node.js application, run:
Open a browser and go to http://localhost:3000
. You should see "Hello, World!".
🐳 Dockerize the Application
Now, let's containerize the Node.js application using Docker.
1. Create a Dockerfile
In the root of your project directory, create a file named Dockerfile
and add the following content:
# Use the official Node.js image as a base image
FROM node:14
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and install dependencies
COPY package*.json ./
RUN npm install
# Copy the application code
COPY . .
# Expose the port the app runs on
EXPOSE 3000
# Start the Node.js application
CMD ["node", "index.js"]
2. Build the Docker Image
Run the following command to build your Docker image:
This command tells Docker to build an image named my-node-app
based on the instructions in your Dockerfile
.
3. Run the Docker Container
After building the image, start a Docker container:
- The
-p 3000:3000
flag maps port 3000 on your local machine to port 3000 inside the container. - Now, go to
http://localhost:3000
in your browser, and you should see "Hello, World!" again, but this time it's being served from within a Docker container.
📈 Summary Table
Here's a quick summary of the tools we installed and the steps we followed:
Tool | Purpose | Link |
---|---|---|
VS Code | Code editor for writing and debugging Node.js apps | Download VS Code |
Node.js | Runtime for executing JavaScript on the server | Download Node.js |
Docker | Containerization platform to standardize the app | Download Docker |
npm | Package manager for Node.js dependencies | Bundled with Node.js |
🚢 What's Next?
You now have a Node.js application running both locally and in a Docker container! 🎉
Next, you could:
- Learn more about Docker volumes and networks to expand your application's capabilities.
- Use Docker Compose to manage multi-container applications (e.g., adding a database).
- Deploy your containerized application to a cloud platform such as AWS, Google Cloud, or Azure.
Happy coding! 👨💻👩💻