53 lines
1.4 KiB
JavaScript
53 lines
1.4 KiB
JavaScript
const express = require("express")
|
|
const app = express()
|
|
const PORT = 3000
|
|
|
|
const path = require('path')
|
|
const formidable = require('formidable')
|
|
const fs = require('fs')
|
|
|
|
app.use(express.json())
|
|
|
|
app.use(express.urlencoded({
|
|
extended: true
|
|
}));
|
|
|
|
app.post('/handleUpload', function(req, res) {
|
|
let form = formidable({})
|
|
|
|
form.keepExtensions = true
|
|
|
|
form.uploadDir = __dirname + '/rzeczy/upload/'
|
|
|
|
form.parse(req, function(err, fields, files) {
|
|
// tu dopiszemy rzeczy
|
|
const sciezka = files.file.path
|
|
const nazwa_pliku_dla_klienta = "/upload" + sciezka.substr(sciezka.lastIndexOf("/"), sciezka.length - sciezka.lastIndexOf("/"))
|
|
|
|
if (files.file.type.substr(0, 5) == "image") {
|
|
const odpowiedz = {
|
|
nazwa_pliku: nazwa_pliku_dla_klienta,
|
|
typ: "obrazek"
|
|
}
|
|
|
|
res.send(JSON.stringify(odpowiedz))
|
|
} else {
|
|
fs.readFile(files.file.path, function(err, data) {
|
|
const odpowiedz = {
|
|
nazwa_pliku: nazwa_pliku_dla_klienta,
|
|
typ: "tekst",
|
|
tresc: data.toString()
|
|
}
|
|
|
|
res.send(JSON.stringify(odpowiedz))
|
|
})
|
|
}
|
|
})
|
|
})
|
|
|
|
app.use(express.static('rzeczy'))
|
|
|
|
app.listen(PORT, function () {
|
|
console.log("start serwera na porcie " + PORT )
|
|
})
|