[feat] node hosting support
This commit is contained in:
148
server/node/server.cjs
Normal file
148
server/node/server.cjs
Normal file
@@ -0,0 +1,148 @@
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
const path = require('path');
|
||||
const htmlparser = require('node-html-parser');
|
||||
const { existsSync, mkdirSync } = require('fs');
|
||||
const bodyParser = require('body-parser');
|
||||
const fs = require('fs/promises')
|
||||
|
||||
app.use(express.static(path.join(process.cwd(), 'dist'), {index: false}));
|
||||
app.use(bodyParser.json({ limit: 100000000 }));
|
||||
|
||||
const savePath = path.join(process.cwd(), "save")
|
||||
if(!existsSync(savePath)){
|
||||
mkdirSync(savePath)
|
||||
}
|
||||
|
||||
app.get('/', async (req, res, next) => {
|
||||
console.log("connected")
|
||||
try {
|
||||
const mainIndex = await fs.readFile(path.join(process.cwd(), 'dist', 'index.html'))
|
||||
const root = htmlparser.parse(mainIndex)
|
||||
const head = root.querySelector('head')
|
||||
head.innerHTML = `<script>globalThis.__NODE__ = true</script>` + head.innerHTML
|
||||
res.send(root.toString())
|
||||
} catch (error) {
|
||||
console.log(error)
|
||||
next(error)
|
||||
}
|
||||
})
|
||||
|
||||
app.post('/proxy', async (req, res, next) => {
|
||||
const urlParam = req.query.url;
|
||||
|
||||
if (!urlParam) {
|
||||
res.status(400).send({
|
||||
error:'URL has no param'
|
||||
});
|
||||
return;
|
||||
}
|
||||
const header = req.headers['risu-header'] ? JSON.parse(decodeURIComponent(req.headers['risu-header'])) : req.headers;
|
||||
|
||||
let originalResponse;
|
||||
try {
|
||||
console.log(urlParam)
|
||||
originalResponse = await fetch(urlParam, {
|
||||
method: req.method,
|
||||
headers: header,
|
||||
body: JSON.stringify(req.body)
|
||||
});
|
||||
|
||||
} catch (err) {
|
||||
next(err);
|
||||
return;
|
||||
}
|
||||
const status = originalResponse.status;
|
||||
|
||||
const originalBody = await originalResponse.text();
|
||||
|
||||
if(status < 200 || status >= 300){
|
||||
res.status(status)
|
||||
}
|
||||
res.send(originalBody);
|
||||
});
|
||||
|
||||
app.get('/api/read', async (req, res, next) => {
|
||||
const filePath = req.headers['file-path'];
|
||||
if (!filePath) {
|
||||
res.status(400).send({
|
||||
error:'File path required'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
if(!existsSync(path.join(savePath, filePath))){
|
||||
res.send({
|
||||
success: true,
|
||||
content: null
|
||||
});
|
||||
}
|
||||
else{
|
||||
const data = await fs.readFile(path.join(savePath, filePath));
|
||||
res.send({
|
||||
success: true,
|
||||
content: data.toString('base64')
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/remove', async (req, res, next) => {
|
||||
const filePath = req.headers['file-path'];
|
||||
if (!filePath) {
|
||||
res.status(400).send({
|
||||
error:'File path required'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await fs.rm(path.join(savePath, filePath));
|
||||
res.send({
|
||||
success: true,
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
app.get('/api/list', async (req, res, next) => {
|
||||
try {
|
||||
const data = (await fs.readdir(path.join(savePath))).map((v) => {
|
||||
return Buffer.from(v, 'hex').toString('utf-8')
|
||||
})
|
||||
res.send({
|
||||
success: true,
|
||||
content: data
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/write', async (req, res, next) => {
|
||||
const filePath = req.headers['file-path'];
|
||||
const fileContent = Buffer.from(req.body.content, 'base64');
|
||||
if (!filePath || !fileContent) {
|
||||
res.status(400).send({
|
||||
error:'File path required'
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await fs.writeFile(path.join(savePath, filePath), fileContent);
|
||||
res.send({
|
||||
success: true
|
||||
});
|
||||
} catch (error) {
|
||||
next(error);
|
||||
}
|
||||
});
|
||||
|
||||
app.listen(6001, () => {
|
||||
console.log("Server is listening on http://localhost:6001/");
|
||||
});
|
||||
Reference in New Issue
Block a user