If I use const http = require('http'); then the upload works correctly, the files are transferred, but if I change it to const https = require('https') then it no longer works and throws code 500.
I use the multer library. The uploads directory is where the app.js server file is, and permissions are 755. Keys pem are added. sd
My HTTPS Code:
const fs = require('fs');const https = require('https'); // Zmiana na 'https'const express = require('express');const multer = require('multer');const path = require('path');const options = { key: fs.readFileSync(path.join(__dirname, './cert/key.pem')), cert: fs.readFileSync(path.join(__dirname, './cert/cert.pem'))}const app = express();const server = https.createServer(options, app); // Zmiana na HTTPS serverconst port = 24006;// Konfiguracja miejsca zapisu plikówconst storage = multer.diskStorage({ destination: function(req, file, cb) { cb(null, 'uploads/'); // Upewnij się, że katalog 'uploads' istnieje }, filename: function(req, file, cb) { cb(null, file.fieldname +'-'+ Date.now() + path.extname(file.originalname)); }});// Inicjalizacja multer z konfiguracją zapisuconst upload = multer({ storage: storage });// Endpoint POST do przesyłania plikówapp.post('/upload', upload.single('file'), (req, res) => { if (!req.file) { return res.status(400).send('No file uploaded.'); } res.send(`File uploaded successfully: ${req.file.filename}`);});// Start serweraserver.listen(port, () => { console.log(`Server is running on https://localhost:${port}`); // Zmiana na HTTPS URL});
Index.html
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Machine Status</title><link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet"><link href="styles.css" rel="stylesheet"></head><body><form id="uploadForm" enctype="multipart/form-data"><input type="file" name="file" accept="*/*"><button type="submit">Upload File</button></form><script> document.getElementById('uploadForm').addEventListener('submit', function(e) { e.preventDefault(); // Zapobieganie domyślnej akcji formularza const formData = new FormData(this); fetch('/upload', { method: 'POST', body: formData }) .then(response => response.text()) .then(data => { console.log('Success:', data); }) .catch((error) => { console.error('Error:', error); });});</script></body></html>