Commit be494a33 authored by wsn's avatar wsn

feat: 初始化 beautiful-hello-world 项目

添加基础项目文件包括 package.json、Dockerfile、运行脚本和服务器代码
parents
Pipeline #65 canceled with stages
# 使用官方 Node.js 轻量级镜像
FROM node:18-alpine
# 设置工作目录
WORKDIR /app
# 复制 package.json
COPY package.json ./
# 安装依赖 (虽然目前没有依赖,但保留此步骤以备将来扩展)
RUN npm install
# 复制源代码
COPY . .
# 暴露端口
EXPOSE 6666
# 启动应用
CMD [ "npm", "start" ]
{
"name": "beautiful-hello-world",
"version": "1.0.0",
"description": "A beautiful Hello World node.js app",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {}
}
#!/bin/bash
# 遇到错误时停止脚本
set -e
# 定义应用名称和端口
APP_NAME="beautiful-hello-world"
PORT=6666
echo "🚀 开始部署 $APP_NAME..."
# 1. 检查 Docker 是否安装
if ! command -v docker &> /dev/null; then
echo "❌ 错误: 未检测到 Docker。请先安装 Docker。"
exit 1
fi
# 2. 构建 Docker 镜像
echo "📦 正在构建 Docker 镜像..."
docker build -t $APP_NAME .
# 3. 停止并删除旧容器(如果存在)
if [ "$(docker ps -aq -f name=$APP_NAME-container)" ]; then
echo "🛑 停止旧容器..."
docker stop $APP_NAME-container
echo "🗑️ 删除旧容器..."
docker rm $APP_NAME-container
fi
# 4. 运行新容器
echo "▶️ 启动新容器..."
# -d: 后台运行
# -p: 端口映射 (主机端口:容器端口)
# --name: 指定容器名称
# -e: 设置环境变量
docker run -d \
-p $PORT:$PORT \
--name $APP_NAME-container \
-e PORT=$PORT \
$APP_NAME
echo "✅ 部署成功!"
echo "🌍 访问地址: http://localhost:$PORT"
const http = require('http');
const html = `
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Hello World</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
color: white;
}
.container {
text-align: center;
background: rgba(255, 255, 255, 0.1);
backdrop-filter: blur(10px);
padding: 3rem;
border-radius: 20px;
box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
border: 1px solid rgba(255, 255, 255, 0.18);
animation: fadeIn 1.5s ease-out;
}
h1 {
font-size: 4rem;
margin-bottom: 1rem;
text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}
p {
font-size: 1.5rem;
opacity: 0.9;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
</head>
<body>
<div class="container">
<h1>Hello World</h1>
<p>Welcome to your beautiful Node.js app!</p>
</div>
</body>
</html>
`;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(html);
});
const PORT = process.env.PORT || 6666;
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment