add server code
This commit is contained in:
parent
42d41bb5c4
commit
a507d16843
5 changed files with 75 additions and 0 deletions
3
server/.gitignore
vendored
Normal file
3
server/.gitignore
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
package-lock.json
|
||||
node_modules
|
||||
dist
|
20
server/package.json
Normal file
20
server/package.json
Normal file
|
@ -0,0 +1,20 @@
|
|||
{
|
||||
"name": "service-api",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@types/cors": "^2.8.12",
|
||||
"@types/express": "^4.17.17",
|
||||
"typescript": "^4.5.5"
|
||||
},
|
||||
"dependencies": {
|
||||
"cors": "^2.8.5",
|
||||
"express": "^4.18.2",
|
||||
"ytdl-core": "npm:@distube/ytdl-core@^4.11.9"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"start": "node ./dist/index.js"
|
||||
}
|
||||
}
|
24
server/src/download.ts
Normal file
24
server/src/download.ts
Normal file
|
@ -0,0 +1,24 @@
|
|||
import { Router } from "express";
|
||||
const ytdl = require("ytdl-core");
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post("/download", async (req, res) => {
|
||||
try {
|
||||
const url = req.body.url;
|
||||
const type = req.body.type;
|
||||
if (!ytdl.validateURL(url))
|
||||
return res.status(400).json({ message: "Invalid URL", code: 400 });
|
||||
if (type !== "mp3" && type !== "mp4")
|
||||
return res.status(400).json({ message: "Invalid type", code: 400 });
|
||||
res.setHeader("Content-Type", type === "mp3" ? "audio/mpeg" : "video/mp4");
|
||||
ytdl(url, {
|
||||
format: type,
|
||||
filter: type == "mp4" ? "videoandaudio" : "audioonly",
|
||||
}).pipe(res);
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
17
server/src/index.ts
Normal file
17
server/src/index.ts
Normal file
|
@ -0,0 +1,17 @@
|
|||
import express from "express";
|
||||
import cors from "cors";
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(cors());
|
||||
|
||||
app.use("/api", require("./download"));
|
||||
|
||||
app.listen(852, () => {
|
||||
console.log("Server running on port 852");
|
||||
});
|
||||
|
||||
process.on("uncaughtException", err => {
|
||||
console.log(err);
|
||||
});
|
11
server/tsconfig.json
Normal file
11
server/tsconfig.json
Normal file
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"compilerOptions": {
|
||||
"target": "es5",
|
||||
"module": "commonjs",
|
||||
"outDir": "./dist",
|
||||
"esModuleInterop": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"skipLibCheck": true
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue