代码拉取完成,页面将自动刷新
const express = require('express')
const path = require('path')
const fs = require('fs')
const app = new express()
const port = process.env.PORT || 5500 // 设定监听端口
// 日志依赖包
const FileStreamRotator = require('file-stream-rotator')
const logger = require('morgan')
// 跨域
const cors = require('cors')
app.use(cors())
// 引入上传文件相关模块
const fileUpload = require('express-fileupload')
app.use(
fileUpload({
useTempFiles: true, // 使用临时文件
tempFileDir: '/tmp/', // 临时路径
createParentPath: true, // 自动创建 .mv(filePathName) 中指定的目录路径
})
)
app.use(express.json())
app.use(express.urlencoded())
app.use('/static', express.static(path.join(__dirname, 'static')))
// 注册和导入路由模块
app.use('/', require('./src/routes/index'))
app.use('/api/v1', require('./src/routes/v1/upload'))
// 用户相关接口
app.use('/api/v1/user', require('./src/routes/v1/user/register'))
app.use('/api/v1/user', require('./src/routes/v1/user/login'))
app.use('/api/v1/user', require('./src/routes/v1/user/auth'))
app.use('/api/v1/user', require('./src/routes/v1/user/getUserInfoList'))
app.use('/api/v1/user', require('./src/routes/v1/user/update/info'))
app.use('/api/v1/user', require('./src/routes/v1/user/avatar/upload'))
app.use('/api/v1/user', require('./src/routes/v1/user/update/password'))
app.use('/api/v1/user', require('./src/routes/v1/user/user-role/index'))
app.use('/api/v1/user', require('./src/routes/v1/user/user-role/getUserRole'))
// 发送验证码
app.use('/api/v1/user', require('./src/routes/v1/user/send-mail'))
app.use('/api/v1/user', require('./src/routes/v1/user/mail-login'))
app.use(
'/api/v1/user',
require('./src/routes/v1/user/user-role/deleteUserRole')
)
// 获取用户评论点赞列表
app.use('/api/v1/user', require('./src/routes/v1/user/get-user-comment-like'))
// 获取用户资讯点赞列表
app.use('/api/v1/user', require('./src/routes/v1/user/get-user-news-like'))
// 获取用户资讯点击列表
app.use('/api/v1/user', require('./src/routes/v1/user/get-user-news-hits'))
// 菜单相关接口
app.use('/api/v1/menu', require('./src/routes/v1/menu/getMenuListTotal'))
app.use('/api/v1/menu', require('./src/routes/v1/menu/add/addMenu'))
app.use('/api/v1/menu', require('./src/routes/v1/menu/update/index'))
app.use('/api/v1/menu', require('./src/routes/v1/menu/get-child-menu'))
// 角色相关接口
app.use('/api/v1/role', require('./src/routes/v1/role/getRoleListTotal'))
app.use('/api/v1/role', require('./src/routes/v1/role/add/addRole'))
app.use('/api/v1/role', require('./src/routes/v1/role/update/index'))
app.use('/api/v1/role', require('./src/routes/v1/role/role-menu/index'))
app.use('/api/v1/role', require('./src/routes/v1/role/role-menu/getRoleMenu'))
app.use(
'/api/v1/role',
require('./src/routes/v1/role/role-menu/deleteRoleMenu')
)
// 栏目相关接口
app.use('/api/v1/class', require('./src/routes/v1/class/add-class'))
app.use('/api/v1/class', require('./src/routes/v1/class/update-class'))
app.use('/api/v1/class', require('./src/routes/v1/class/get-class'))
app.use('/api/v1/class', require('./src/routes/v1/class/get-child-class'))
// 资讯相关接口
app.use('/api/v1/news', require('./src/routes/v1/news/add-news'))
app.use('/api/v1/news', require('./src/routes/v1/news/get-news'))
app.use('/api/v1/news', require('./src/routes/v1/news/update-news'))
app.use('/api/v1/news', require('./src/routes/v1/news/get-news-content'))
app.use('/api/v1/news', require('./src/routes/v1/news/news-like'))
app.use('/api/v1/news', require('./src/routes/v1/news/get-news-like-list'))
// 评论相关接口
app.use('/api/v1/comment', require('./src/routes/v1/comment/get-comment'))
app.use('/api/v1/comment', require('./src/routes/v1/comment/add-comment'))
app.use(
'/api/v1/comment',
require('./src/routes/v1/comment/get-comment-like-list')
)
app.use('/api/v1/comment', require('./src/routes/v1/comment/comment-like'))
app.use(function (req, res, next) {
return res.status(404).send("Sorry can't find that!")
})
const logDirectory = path.join(__dirname, 'log')
// 确保存储的路径存在
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory)
// 创建输出流
var errorLogStream = FileStreamRotator.getStream({
date_format: 'YYYYMMDD', //日期类型
filename: path.join(logDirectory, '%DATE%-error.log'), //文件名
frequency: 'daily', //每天的频率
verbose: false,
})
// 创建输出流
var accessLogStream = FileStreamRotator.getStream({
date_format: 'YYYYMMDD',
filename: path.join(logDirectory, '%DATE%-access.log'),
frequency: 'daily',
verbose: false,
})
//写正常访问请求的log日志
app.use(
logger(
':localDate :remote-addr :method :url :status :res[content-length] - :response-time ms',
{ stream: accessLogStream }
)
)
//写访问出错的log日志
app.use(
logger(
':localDate :remote-addr :method :url :status :res[content-length] - :response-time ms',
{
skip: function (req, res) {
return res.statusCode < 400
},
stream: errorLogStream,
}
)
)
app.listen(port, function () {
console.log(`服务启动成功 http://localhost:${port}`)
})
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。