๐ข Setting Up Node.js Applications: Simple Guide
Want to run JavaScript on your server? Excellent choice! ๐ This tutorial shows you how to set up Node.js applications on Alpine Linux. Letโs build awesome web apps! ๐
๐ค What are Node.js Applications?
Node.js applications are JavaScript programs that run on servers instead of web browsers.
Node.js applications are like:
- ๐ญ JavaScript factories that process requests
- ๐ค Smart assistants that handle web tasks
- ๐ Bridges connecting web pages to databases
๐ฏ What You Need
Before we start, you need:
- โ Alpine Linux system with internet access
- โ Basic knowledge of terminal commands
- โ Some JavaScript knowledge (helpful but not required)
- โ About 500MB free disk space
๐ Step 1: Install Node.js and NPM
Install Node.js Runtime
Letโs install Node.js and its package manager! ๐
What weโre doing: Installing the JavaScript runtime and tools needed for web applications.
# Update package list
apk update
# Install Node.js and NPM
apk add nodejs npm
# Install additional development tools
apk add git curl
# Check Node.js version
node --version
# Check NPM version
npm --version
# Create apps directory
mkdir -p /opt/nodejs-apps
cd /opt/nodejs-apps
What this does: ๐ Installs complete Node.js development environment.
Example output:
โ
Node.js v18.17.0 installed
โ
NPM v9.6.7 ready
โ
Development tools available
โ
Apps directory created
What this means: Perfect! Node.js is ready for development! โ
๐ก Important Tips
Tip: NPM installs packages automatically - very convenient! ๐ก
Warning: Always check package security before installing! โ ๏ธ
๐ ๏ธ Step 2: Create Your First Node.js App
Build Simple Web Application
Letโs create a basic web app to test everything! ๐
What weโre doing: Making a simple web server that responds to requests.
# Create new app directory
mkdir hello-app
cd hello-app
# Initialize new Node.js project
npm init -y
# Create main application file
cat > app.js << 'EOF'
// Simple Node.js web server
const http = require('http');
const port = 3000;
// Create HTTP server
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(`
<h1>๐ข Hello from Alpine Linux!</h1>
<p>Your Node.js application is working! ๐</p>
<p>Request URL: ${req.url}</p>
<p>Server time: ${new Date().toISOString()}</p>
`);
});
// Start server
server.listen(port, () => {
console.log(`โ
Server running at http://localhost:${port}`);
});
EOF
# View the created files
ls -la
cat package.json
Code explanation:
http.require('http')
: Loads web server modulecreateServer
: Creates new web serverres.writeHead(200)
: Sends success responseserver.listen(port)
: Starts server on port 3000
Expected Output:
โ
New Node.js project initialized
โ
Package.json configuration created
โ
Simple web server code ready
What this means: Great! Your first Node.js app is ready! ๐
๐ฎ Letโs Try It!
Time to run our web application! This is exciting! ๐ฏ
What weโre doing: Starting the web server and testing it works.
# Start the Node.js application
node app.js
# In another terminal, test the server
curl http://localhost:3000
# Or test with different paths
curl http://localhost:3000/about
curl http://localhost:3000/api/test
# Stop server with Ctrl+C when done
You should see:
โ
Server running at http://localhost:3000
โ
HTML response with Node.js message
โ
Server responds to all URL paths
Awesome work! ๐
๐ Quick Summary Table
Component | Purpose | Result |
---|---|---|
๐ข Node.js | JavaScript runtime | โ Server-side JavaScript |
๐ ๏ธ NPM | Package manager | โ Easy library installation |
๐ฏ HTTP module | Web server | โ Handle web requests |
๐ฎ Practice Time!
Letโs build a more advanced app! Try these examples:
Example 1: Express.js Web Framework ๐ข
What weโre doing: Using Express.js framework for easier web development.
# Install Express.js framework
npm install express
# Create Express application
cat > express-app.js << 'EOF'
const express = require('express');
const app = express();
const port = 3000;
// Middleware for JSON parsing
app.use(express.json());
// Routes
app.get('/', (req, res) => {
res.json({
message: '๐ข Express.js on Alpine Linux!',
timestamp: new Date().toISOString(),
status: 'running'
});
});
app.get('/api/health', (req, res) => {
res.json({
status: 'healthy',
uptime: process.uptime(),
memory: process.memoryUsage()
});
});
app.post('/api/echo', (req, res) => {
res.json({
received: req.body,
echo: 'Message received!'
});
});
// Start server
app.listen(port, () => {
console.log(`๐ Express app running on port ${port}`);
});
EOF
# Run Express application
node express-app.js
What this does: Creates professional web API with Express! ๐
Example 2: Process Management with PM2 ๐ก
What weโre doing: Setting up production-ready process management.
# Install PM2 process manager
npm install -g pm2
# Create PM2 configuration
cat > ecosystem.config.js << 'EOF'
module.exports = {
apps: [{
name: 'hello-app',
script: 'app.js',
instances: 2,
exec_mode: 'cluster',
env: {
NODE_ENV: 'production',
PORT: 3000
},
error_file: './logs/err.log',
out_file: './logs/out.log',
log_file: './logs/combined.log'
}]
};
EOF
# Create logs directory
mkdir -p logs
# Start app with PM2
pm2 start ecosystem.config.js
# Check PM2 status
pm2 list
# View logs
pm2 logs hello-app
What this does: Manages Node.js apps like a production server! ๐
๐จ Fix Common Problems
Problem 1: โPort already in useโ Error โ
What happened: Another process is using port 3000. How to fix it: Find and stop the process or use different port!
# Find process using port 3000
netstat -tlnp | grep :3000
# Kill process using the port
kill $(lsof -t -i:3000)
# Or use different port in your app
# Change port = 3000 to port = 3001
Problem 2: โModule not foundโ Error โ
What happened: Required NPM packages not installed. How to fix it: Install missing dependencies!
# Install missing packages
npm install
# Check package.json dependencies
cat package.json
# Install specific package
npm install express
# Update all packages
npm update
Donโt worry! Node.js development has a learning curve. Youโre building web skills! ๐ช
๐ก Simple Tips
- Use package.json ๐ - Always track your dependencies properly
- Handle errors ๐ฑ - Add error handling to prevent crashes
- Environment variables ๐ค - Use config files for different environments
- Monitor processes ๐ช - Use PM2 or similar for production apps
โ Check Everything Works
Letโs verify Node.js setup is working perfectly:
# Check Node.js and NPM versions
node --version
npm --version
# List installed packages
npm list
# Test simple JavaScript
node -e "console.log('โ
Node.js working!')"
# Check running processes
ps aux | grep node
# Test HTTP request
curl -s http://localhost:3000 | head -5
Good output:
โ
Node.js and NPM versions displayed
โ
Packages listed correctly
โ
JavaScript execution works
โ
Node.js processes running
โ
HTTP responses received
๐ What You Learned
Great job! Now you can:
- โ Install and configure Node.js on Alpine Linux
- โ Create simple web applications with JavaScript
- โ Use Express.js framework for web APIs
- โ Manage Node.js processes with PM2!
๐ฏ Whatโs Next?
Now you can try:
- ๐ Building REST APIs with databases
- ๐ ๏ธ Creating real-time applications with WebSockets
- ๐ค Deploying Node.js apps with Docker
- ๐ Building full-stack JavaScript applications!
Remember: Every web developer started with simple Node.js servers. Youโre building modern web applications! ๐
Keep practicing and youโll master server-side JavaScript! ๐ซ