const config = require('./config') const express = require('express') const status = require('http-status') const cors = require('cors') const xss = require('xss-clean') const app = express() const fs = require('fs') const path = require('path') const axios = require('axios').default const fetch = axios.create() // parse json request body app.use(express.json()) // parse urlencoded request body app.use(express.urlencoded({ extended: true })) // sanitize request data app.use(xss()) app.use(express.static(config.template)) app.get('/view', (req, res, next) => { let num = req.query.m if (num) { const view = path.join(config.template, 'view.html') return fetch .get(`https://test.4dkankan.com/scene/data/data${num}/scene.json?_=${Date.now()}`) .then(response => { if (response.status >= 200 && response.status <= 400) { fs.access(view, err => { if (err) { res.statusCode = status.INTERNAL_SERVER_ERROR res.end() console.error(err) } else { fs.readFile(view, (err, html) => { if (err) { res.statusCode = status.INTERNAL_SERVER_ERROR res.end() console.error(err) } else { const description = response.data.sceneDec.replace(/<[^>]+>/gi, '') html = html.toString() html = html.replace('', `${response.data.sceneName}`) html = html.replace('', ``) html = html.replace('', ``) html = html.replace('', ``) html = html.replace('', ``) html = html.replace('', ``) html = html.replace('', ``) html = html.replace( '', `` ) html = html.replace('', ``) html = html.replace('', ``) html = html.replace('', ``) html = html.replace('window.__KANKAN_DATA = null', 'window.__KANKAN_DATA = ' + JSON.stringify(response.data)) res.end(html) } }) } }) } else { next() } }) .catch(err => { res.statusCode = err.response.status res.end() }) } res.statusCode = status.NOT_FOUND res.end() }) if (config.env == 'development') { // enable cors app.use(cors()) app.options('*', cors()) } const logger = { info(msg) { console.log(msg) }, error(msg) { console.log(msg) }, } const server = app.listen(config.port, () => { logger.info(`Listening to port ${config.port}`) }) const exitHandler = () => { if (server) { server.close(() => { logger.info('Server closed') process.exit(1) }) } else { process.exit(1) } } const unexpectedErrorHandler = error => { logger.error(error) exitHandler() } process.on('uncaughtException', unexpectedErrorHandler) process.on('unhandledRejection', unexpectedErrorHandler) process.on('SIGTERM', () => { logger.info('SIGTERM received') if (server) { server.close() } })