1 Star 0 Fork 0

weizu_cool/GifSource

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
server.js 2.01 KB
一键复制 编辑 原始数据 按行查看 历史
weizu_cool 提交于 2022-01-08 19:59 . add
var http = require('http')
var fs = require('fs')
const router = require('./router')
const imgscan = require('./utils/scanImgFile')
const generateCode = require('./utils/generateCode')
const struils = require('./utils/stringUtil')
// 默认显示当天的图片
var showSetting = 'day'
function minusDays(DateA, DateB) {
var time1 = Date.parse(DateA)
var time2 = Date.parse(DateB)
var nDays = Math.abs(parseInt((time2 - time1) / 1000 / 3600 / 24))
return nDays
}
/**
* 根据日期判断哪些文件当前需要加载
* keys 传入扫描过的文件路径
*/
function getFileByDate(keys) {
var daylist = []
var weeklist = []
var monthlist = []
var yearlist = []
for (var i = 0; keys != undefined && keys != null && i < keys.length; i++) {
var key = keys[i]
stats = fs.statSync(__dirname + '/images/' + key)
if (stats) {
var fileDate = new Date(stats['birthtime'])
var currentDate = new Date()
var days = minusDays(currentDate, fileDate)
if (days < 1) {
daylist.push(key)
} else if (days < 7) {
weeklist.push(key)
}
if (days < 30) {
monthlist.push(key)
}
if (days < 365) {
yearlist.push(key)
}
}
}
return {
day: daylist,
week: weeklist,
month: monthlist,
year: yearlist,
}
}
function start() {
const server = http.createServer(function (req, res) {
var url = ''
if (struils.trim(req.url) == '/') url = ''
else url = struils.trim(req.url.substring(1))
var links = url.split('?')
if (links.length != 1) {
showSetting = links[1].split('=')[1]
}
console.log(url)
// 显示内容
router.processRoute(req, res)
// 扫描文件
var keys = imgscan.scan(req, res)
// 根据文件日期丢弃部分数据
var datas = getFileByDate(keys)
if (keys.length != 0) {
generateCode.process(datas, req.url)
}
})
server.listen(3001, 'localhost', () => {
console.log(`Server running at http://localhost:3001/`)
})
}
exports.start = start
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/weizu_cool/gif-source.git
git@gitee.com:weizu_cool/gif-source.git
weizu_cool
gif-source
GifSource
master

搜索帮助