-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
32 lines (26 loc) · 844 Bytes
/
app.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
const express = require('express');
const ytdl = require('ytdl-core');
const app = express();
const {ytDownloader} = require('./downloader')
const port = 3000;
app.set('view engine', 'ejs')
app.use(express.static('./views'))
app.use(express.static('./public'))
app.use(express.json())
app.use(express.urlencoded({extended: true}))
app.get('/', (req, res) => {
res.render("index")
});
app.post('/download', async (req, res) => {
const {url, type} = req.body
const result = await ytDownloader(url, type);
if (result.status === 200) {
res.header('Content-Disposition', `attachment; filename="${result.fileName}"`);
result.stream.pipe(res)
} else {
res.status(result.status).send(result.message);
}
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});