-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
184 lines (158 loc) · 6.06 KB
/
index.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import formidable from "formidable";
import express from "express";
import fs from "fs";
import http from "http";
import https from "https";
import cors from "cors";
import path from "path";
import os from "os";
import { fileURLToPath } from "url";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Check if dist folder exists
const distPath = path.join(__dirname, "../dist");
if (!fs.existsSync(distPath)) {
console.error("\x1b[31m%s\x1b[0m", "Error: 'dist' folder not found!");
console.log("\nPlease build the project first by running:");
console.log("\x1b[33m%s\x1b[0m", "npm run build");
console.log("\nThen try running the server again.\n");
process.exit(1);
}
const app = express();
app.use(
cors({
origin: (origin, callback) => {
return callback(null, true);
},
})
);
// Serve static files
app.use("/dist", express.static(distPath));
app.use("/assets", express.static(path.join(__dirname, "../samples/demo/assets")));
app.use("/css", express.static(path.join(__dirname, "../samples/demo/css")));
app.use("/font", express.static(path.join(__dirname, "../samples/demo/font")));
// Routes
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/hello-world.html"));
});
app.get("/demo", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/demo/index.html"));
});
app.get("/hello-world", (req, res) => {
res.sendFile(path.join(__dirname, "../samples/hello-world.html"));
});
// Allow upload feature
app.post("/upload", function (req, res) {
try {
// Create a new Formidable form
const form = formidable({
multiples: false,
keepExtensions: true,
});
form.parse(req, (err, fields, files) => {
if (err) {
console.error(err);
return res.status(500).send("Error processing the file upload.");
}
const uploadedFile = files.uploadFile[0]; // Ensure the file field name matches the form
if (!uploadedFile) {
return res.status(400).json({ success: false, message: "No file uploaded" });
}
// Get current timestamp
let dt = new Date();
const fileSavePath = path.join(__dirname, "\\");
const newFileName = uploadedFile.originalFilename;
const newFilePath = path.join(fileSavePath, newFileName);
// Move the uploaded file to the desired directory
fs.rename(uploadedFile.filepath, newFilePath, (err) => {
if (err) {
console.error(err);
return res.status(500).send("Error saving the file.");
}
console.log(`\x1b[33m ${newFileName} \x1b[0m uploaded successfully!`);
});
res.status(200).json({
success: true,
message: `${newFileName} uploaded successfully`,
filename: newFileName,
});
});
} catch (error) {
res.status(500).send("An error occurred during file upload.");
}
});
let httpPort = 3000;
let httpsPort = 3001;
// redirect handling
app.use((req, res, next) => {
const host = req.get("Host"); // Get the host name from the request
// Skip redirection if it's localhost with the correct HTTP port
if (!req.secure && host !== `localhost:${httpPort}`) {
// Replace the HTTP port with HTTPS port in the host
const httpsHost = host.replace(`:${httpPort}`, `:${httpsPort}`);
return res.redirect(["https://", httpsHost, req.url].join(""));
}
next(); // Proceed to the next middleware or route
});
// HTTPS server configuration
const httpsOptions = {
key: fs.readFileSync(path.join(__dirname, "pem/key.pem")),
cert: fs.readFileSync(path.join(__dirname, "pem/cert.pem")),
};
// Create HTTPS server
const httpsServer = https.createServer(httpsOptions, app);
// Create HTTP server
const httpServer = http.createServer(app);
// Add error handlers before starting servers
httpServer.on("error", (error) => {
if (error.code === "EADDRINUSE") {
console.error(`\x1b[31mError: Port ${httpPort} is already in use\x1b[0m`);
console.log("\nTo fix this, you can:");
console.log(`1. Update the port manually by changing \x1b[33mhttpPort\x1b[0m in the code`);
console.log(`2. Close any other applications using port ${httpPort}`);
console.log(`3. Wait a few moments and try again - the port might be in a cleanup state\n`);
} else {
console.error("\x1b[31mHTTP Server error:\x1b[0m", error);
}
process.exit(1);
});
httpsServer.on("error", (error) => {
if (error.code === "EADDRINUSE") {
console.error(`\x1b[31mError: Port ${httpsPort} is already in use\x1b[0m`);
console.log("\nTo fix this, you can:");
console.log(`1. Update the port manually by changing \x1b[33mhttpsPort\x1b[0m in the code`);
console.log(`2. Close any other applications using port ${httpsPort}`);
console.log(`3. Wait a few moments and try again - the port might be in a cleanup state\n`);
} else {
console.error("\x1b[31mHTTP Server error:\x1b[0m", error);
}
process.exit(1);
});
// Start the servers
httpServer.listen(httpPort, () => {
console.log("\n\x1b[1m Dynamsoft Document Scanner Samples\x1b[0m\n");
console.log("\x1b[36m HTTP URLs:\x1b[0m");
console.log("\x1b[90m-------------------\x1b[0m");
console.log("\x1b[33m Hello World:\x1b[0m http://localhost:" + httpPort + "/hello-world");
console.log("\x1b[33m Demo:\x1b[0m http://localhost:" + httpPort + "/demo");
});
httpsServer.listen(httpsPort, "0.0.0.0", () => {
const networkInterfaces = os.networkInterfaces();
const ipv4Addresses = [];
Object.keys(networkInterfaces).forEach((interfaceName) => {
networkInterfaces[interfaceName].forEach((iface) => {
if (iface.family === "IPv4" && !iface.internal) {
ipv4Addresses.push(iface.address);
}
});
});
console.log("\n");
console.log("\x1b[36m HTTPS URLs:\x1b[0m");
console.log("\x1b[90m-------------------\x1b[0m");
ipv4Addresses.forEach((localIP) => {
console.log("\x1b[32m Hello World:\x1b[0m https://" + localIP + ":" + httpsPort + "/hello-world");
console.log("\x1b[32m Demo:\x1b[0m https://" + localIP + ":" + httpsPort + "/demo");
});
console.log("\n");
console.log("\x1b[90mPress Ctrl+C to stop the server\x1b[0m\n");
});