-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
durunsong
committed
Nov 26, 2024
1 parent
57398c4
commit fcfc745
Showing
14 changed files
with
517 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# 这里放置一些配置信息 | ||
PORT = 4000 | ||
# JWT 刷新令牌KEY | ||
# 端口号 | ||
PORT = '4000' | ||
# JWT_SECRET --- 用于生成和验证JWT的密钥 | ||
JWT_SECRET = 'f7d623cd21149c493d7304960edaf2e10ad147528dbaf183520184fc0a0f64cb' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/** | ||
* 引入模块依赖 | ||
*/ | ||
const app = require("./bin/index"); | ||
const debug = require("debug")("kilyicms_server:server"); | ||
const http = require("http"); | ||
|
||
/** | ||
* 从环境变量中获取端口号,如果未定义则使用默认端口 4000 | ||
*/ | ||
const port = normalizePort(process.env.PORT || "4000"); | ||
app.set("port", port); | ||
|
||
/** | ||
* 创建 HTTP 服务器 | ||
*/ | ||
const server = http.createServer(app); | ||
|
||
/** | ||
* 监听指定端口,启动服务器 | ||
*/ | ||
server.listen(port, () => { | ||
console.log(`Server is running on port ${port}`); | ||
}); | ||
server.on("error", onError); | ||
server.on("listening", onListening); | ||
|
||
/** | ||
* 将端口规范化为数字、字符串或 false | ||
* @param {string|number} val - 输入的端口值 | ||
* @returns {number|string|boolean} - 规范化后的端口 | ||
*/ | ||
function normalizePort(val) { | ||
const port = parseInt(val, 10); | ||
// 检查是否为有效端口号 | ||
if (isNaN(port)) { | ||
// 不是数字则返回原始字符串(如命名管道) | ||
return val; | ||
} | ||
if (port >= 0) { | ||
// 有效端口号 | ||
return port; | ||
} | ||
return false; // 无效端口 | ||
} | ||
|
||
/** | ||
* 处理服务器启动过程中的错误 | ||
* @param {Error} error - 错误对象 | ||
*/ | ||
function onError(error) { | ||
if (error.syscall !== "listen") { | ||
throw error; | ||
} | ||
const bind = typeof port === "string" ? "Pipe " + port : "Port " + port; | ||
// 用友好信息处理特定的监听错误 | ||
switch (error.code) { | ||
case "EACCES": | ||
console.error(bind + " requires elevated privileges"); | ||
process.exit(1); | ||
break; | ||
case "EADDRINUSE": | ||
console.error(bind + " is already in use"); | ||
process.exit(1); | ||
break; | ||
default: | ||
throw error; | ||
} | ||
} | ||
|
||
/** | ||
* HTTP 服务器 “监听 ”事件的事件监听器。 | ||
*/ | ||
function onListening() { | ||
const addr = server.address(); | ||
const bind = typeof addr === "string" ? "pipe " + addr : "port " + addr.port; | ||
debug("Listening on " + bind); | ||
} | ||
|
||
/** | ||
* @vercel部署需要这样写 | ||
*/ | ||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 入口文件 | ||
require("dotenv").config(); | ||
const createError = require("http-errors"); | ||
const express = require("express"); | ||
const bodyParserMiddleware = require("../middleware/bodyParserMiddleware"); | ||
const corsMiddleware = require("../middleware/corsMiddleware"); | ||
const userRoutes = require("../routes/userRoutes"); | ||
const { connectDb } = require("../config/db-connection"); | ||
const path = require("path"); | ||
// const { hashExistingPasswords } = require("./controllers/hashExistingPasswords"); | ||
|
||
// 创建服务器对象 | ||
const app = express(); | ||
|
||
// 在服务器启动时,调用 hashExistingPasswords 函数 | ||
// 备用方案, 如果需要加密所有用户密码,则取消注释 | ||
// hashExistingPasswords(); | ||
|
||
// 加载中间件 | ||
app.use(bodyParserMiddleware.json); // 使用 bodyParser.json() | ||
app.use(bodyParserMiddleware.urlencoded); // 使用 bodyParser.urlencoded() | ||
app.use(corsMiddleware); // 使用 cors() | ||
|
||
// 设置静态资源目录 | ||
app.use(express.static(path.join(__dirname, "../public"))); | ||
|
||
// 设置视图引擎 | ||
app.set("views", path.join(__dirname, "../views")); | ||
app.set("view engine", "ejs"); | ||
|
||
// 数据库连接 | ||
connectDb(); | ||
|
||
// 注册路由 | ||
app.use("/", userRoutes); | ||
|
||
// 处理 404 错误 | ||
app.use((req, res, next) => { | ||
next(createError(404)); | ||
}); | ||
|
||
// 错误处理 | ||
app.use((err, req, res) => { | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get("env") === "development" ? err : {}; | ||
res.status(err.status || 500).render("error"); | ||
}); | ||
|
||
module.exports = app; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// 默认首页 | ||
const homePage = (req, res) => { | ||
res.send(` | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<link rel="icon" href="/images/api.png" type="image/x-icon" /> | ||
<title>kilyicms-server</title> | ||
<style> | ||
body { | ||
margin: 0; | ||
height: 100vh; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
background-color: #f4f4f9; | ||
} | ||
h1 { | ||
font-size: 3rem; | ||
font-weight: bold; | ||
background: linear-gradient(45deg, #ff416c, #ff4b2b, #f0c27b, #4b1248); | ||
-webkit-background-clip: text; | ||
-webkit-text-fill-color: transparent; | ||
text-align: center; | ||
animation: gradient 5s infinite; | ||
} | ||
@keyframes gradient { | ||
0% { background-position: 0% 50%; } | ||
50% { background-position: 100% 50%; } | ||
100% { background-position: 0% 50%; } | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>Welcome to the world of kilyicms API!</h1> | ||
</body> | ||
</html> | ||
`); | ||
}; | ||
|
||
module.exports = { | ||
homePage, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.