-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
70 lines (61 loc) · 1.78 KB
/
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
require('dotenv').config()
const express = require('express')
const path = require('path')
const axios = require('axios')
const fs = require('fs')
const multer = require('multer')
const methodoverride = require('method-override')
const FormData = require('form-data')
const cors = require('cors')
const lighthouse = require('@lighthouse-web3/sdk')
const app = express()
app.use(cors())
const port = 8080
app.use(express.static(path.join(__dirname, '/public')))
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(methodoverride('_method'))
const API_KEY = process.env.LIGHTHOUSE_API_KEY
const uploadsDir = path.join(__dirname, 'uploads')
if (!fs.existsSync(uploadsDir)) {
fs.mkdirSync(uploadsDir)
}
const upload = multer({ dest: uploadsDir })
app.listen(port, () => {
console.log(`Listening on port ${port}`)
})
app.post('/uploadDocument', upload.single('file'), async (req, res) => {
const file = req.file
console.log(file)
if (!file) {
return res.status(400).json({ error: 'No file uploaded' })
}
const filePath = file.path
try {
const uploadResponse = await lighthouse.upload(filePath, API_KEY)
console.log(uploadResponse)
res.json({ uploadResponse })
} catch (error) {
console.log(error)
}
})
app.post('/uploadText', async (req, res) => {
console.log(req.body)
const text = req.body.text
let text2
try {
const parsedText = JSON.parse(text)
text2 = JSON.stringify(parsedText)
} catch (e) {
// If parsing fails, it means text is not valid JSON, so don't stringify it
text2 = text
}
const name = req.body.name
try {
const uploadResponse = await lighthouse.uploadText(text2, API_KEY, name)
console.log(uploadResponse)
res.json({ uploadResponse })
} catch (error) {
console.log(error)
}
})