-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (33 loc) · 1.17 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
'use strict';
const express = require('express');
const path = require('path');
const serverless = require('serverless-http');
const app = express();
const bodyParser = require('body-parser');
const ytdl = require('ytdl-core');
const router = express.Router();
router.get('/', (req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<h1>Hello from Express.js!</h1>');
res.end();
});
router.get('/another', (req, res) => res.json({ route: req.originalUrl }));
router.post('/', (req, res) => res.json({ postBody: req.body }));
app.use(bodyParser.json());
app.use('/.netlify/functions/server', router); // path must route to lambda
//fiturnya
app.get('/download', async (req, res) => {
try {
const videoURL = req.query.url;
const info = await ytdl.getInfo(videoURL);
const format = ytdl.chooseFormat(info.formats, { quality: 'highestaudio' });
res.header('Content-Disposition', `attachment; filename="${info.title}.mp3"`);
ytdl(videoURL, { format: format })
.pipe(res);
} catch (error) {
console.error(error);
res.status(500).send('Error during download');
}
});
module.exports = app;
module.exports.handler = serverless(app);