Files
2026-09-24 10:51:42 +02:00

50 lines
1.4 KiB
JavaScript

const express = require("express")
const app = express()
const PORT = 3000
const path = require('path')
const fs = require('fs')
const formidable = require('formidable')
app.get("/", function (req, res) {
res.sendFile(path.join(__dirname, '/static/form5.html'))
})
app.post('/process_upload', function (req, res) {
let formularz = formidable({
keepExtensions: true
})
formularz.uploadDir = __dirname + '/static/upload/'
formularz.parse(req, function (err, fields, files) {
// https://stackoverflow.com/questions/423376/how-to-get-the-file-name-from-a-full-path-using-javascript
let name_from_path = files.wyslany_plik.path.replace(/^.*[\\/]/, '')
if (files.wyslany_plik.type == 'text/plain') {
fs.readFile(files.wyslany_plik.path, function (err, data) {
let danepliku = {
"type": files.wyslany_plik.type,
"path": name_from_path,
"text": data.toString()
}
res.send(JSON.stringify(danepliku))
})
} else {
let danepliku = {
"type": files.wyslany_plik.type,
"path": name_from_path,
"text": ""
}
res.send(JSON.stringify(danepliku))
}
})
})
app.use(express.static('static'))
app.listen(PORT, function () {
console.log("start serwera na porcie " + PORT)
})