Create index.js
Browse files
index.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import express from 'express'
|
| 2 |
+
import shellExec from 'express-shell-exec'
|
| 3 |
+
import morgan from 'morgan'
|
| 4 |
+
import puppeteer from 'puppeteer-ghost'
|
| 5 |
+
import serveIndex from 'serve-index'
|
| 6 |
+
|
| 7 |
+
import { tmpdir } from 'node:os'
|
| 8 |
+
import { format } from 'node:util'
|
| 9 |
+
|
| 10 |
+
const app = express()
|
| 11 |
+
|
| 12 |
+
app.set('json spaces', 2)
|
| 13 |
+
app.use(express.json({ limit: '200mb' }))
|
| 14 |
+
app.use(express.urlencoded({
|
| 15 |
+
extended: true,
|
| 16 |
+
limit: '200mb'
|
| 17 |
+
}))
|
| 18 |
+
app.use(morgan('combined'))
|
| 19 |
+
app.use(shellExec.middleware())
|
| 20 |
+
|
| 21 |
+
const tmpDir = tmpdir()
|
| 22 |
+
app.use(
|
| 23 |
+
tmpDir,
|
| 24 |
+
express.static(tmpDir),
|
| 25 |
+
serveIndex(
|
| 26 |
+
tmpDir,
|
| 27 |
+
{ hidden: true, icons: true }
|
| 28 |
+
)
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
app.all(
|
| 32 |
+
'/',
|
| 33 |
+
(_, res) => res.json({ msg: 'Hello World' })
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
app.get('/ss', async (req, res) => {
|
| 37 |
+
const { url = process.env.SPACE_HOST } = req.query
|
| 38 |
+
const browser = await puppeteer.launch({
|
| 39 |
+
executablePath: process.env.CHROME_BIN,
|
| 40 |
+
headless: 'new'
|
| 41 |
+
})
|
| 42 |
+
try {
|
| 43 |
+
const page = await browser.newPage()
|
| 44 |
+
await page.goto(url)
|
| 45 |
+
const name =
|
| 46 |
+
tmpDir + '/' +
|
| 47 |
+
Math.random().toString(36).slice(2) + '.jpg'
|
| 48 |
+
const ss = await page.screenshot({ path: name })
|
| 49 |
+
res.status(200).redirect(name)
|
| 50 |
+
} catch (e) {
|
| 51 |
+
console.error(e)
|
| 52 |
+
res
|
| 53 |
+
.status(500)
|
| 54 |
+
.json({ error: true, msg: format(e) })
|
| 55 |
+
} finally {
|
| 56 |
+
await browser.close()
|
| 57 |
+
}
|
| 58 |
+
})
|
| 59 |
+
|
| 60 |
+
const PORT = process.env.PORT || 7860
|
| 61 |
+
app.listen(
|
| 62 |
+
PORT,
|
| 63 |
+
() => console.log('App running on port', PORT)
|
| 64 |
+
)
|