var createError = require('http-errors')var express = require('express')var path = require('path')var cookieParser = require('cookie-parser')var logger = require('morgan')const { verifyTokenMiddle } = require('./auth/token')const expressSession = require('express-session')const redis = require('redis')const { redisConfig } = require('./config/config')const redisClient = redis.createClient(redisConfig)const RedisStore = require('connect-redis')(expressSession)redisClient.on('connect', function () { console.log('Redis client connected')})redisClient.on('error', function (e) { console.error(e)})var app = express()app.use(express.static(path.join(__dirname, '/public')))app.all('*', function (req, res, next) { res.header('Access-Control-Allow-Origin', '*') res.header('Access-Control-Allow-Headers', 'Content-Type') res.header('Access-Control-Allow-Methods', '*') res.header('Content-Type', 'application/json;charset=utf-8') if (req.method === 'OPTIONS') { res.end() } else { next() }})app.use(cookieParser())app.use( expressSession({ store: new RedisStore({ client: redisClient }), name: 'session_id', secret: 'yupi996', resave: true, saveUninitialized: true, rolling: true, cookie: { domain: '.mianshiya.com', maxAge: 1000 * 60 * 60 * 24 * 15, httpOnly: true, }, }))app.use(express.json())app.use(express.urlencoded({ extended: false }))app.use(verifyTokenMiddle)setRoutes()app.use(function (req, res, next) { next(createError(404))})app.use(function (err, req, res, next) { console.log(2) res.locals.message = err.message res.locals.error = req.app.get('env') === 'development' ? err : {} res.status(err.status || 500) res.send('error')})function setRoutes() { const API = '/api/' const fileDir = 'routes' const noIncludePath = ['index'] const getPrefix = (path) => path.split('.')[0] require('fs') .readdirSync(path.join(__dirname, fileDir)) .filter((path) => noIncludePath.some((noPath) => !path.startsWith(noPath))) .forEach((api) => { app.use( API + getPrefix(api), require(path.join(__dirname, fileDir + '/') + getPrefix(api)) ) })}module.exports = app