1+ import formidable from "formidable" ;
12import express from "express" ;
23import fs from "fs" ;
34import http from "http" ;
@@ -32,23 +33,69 @@ app.use(
3233
3334// Serve static files
3435app . use ( "/dist" , express . static ( distPath ) ) ;
35- app . use ( "/assets" , express . static ( path . join ( __dirname , "../samples/assets" ) ) ) ;
36- app . use ( "/css" , express . static ( path . join ( __dirname , "../samples/css" ) ) ) ;
37- app . use ( "/font" , express . static ( path . join ( __dirname , "../samples/font" ) ) ) ;
36+ app . use ( "/assets" , express . static ( path . join ( __dirname , "../samples/demo/ assets" ) ) ) ;
37+ app . use ( "/css" , express . static ( path . join ( __dirname , "../samples/demo/ css" ) ) ) ;
38+ app . use ( "/font" , express . static ( path . join ( __dirname , "../samples/demo/ font" ) ) ) ;
3839
3940// Routes
4041app . get ( "/" , ( req , res ) => {
41- res . sendFile ( path . join ( __dirname , "../samples/demo .html" ) ) ;
42+ res . sendFile ( path . join ( __dirname , "../samples/hello-world .html" ) ) ;
4243} ) ;
4344
4445app . get ( "/demo" , ( req , res ) => {
45- res . sendFile ( path . join ( __dirname , "../samples/demo.html" ) ) ;
46+ res . sendFile ( path . join ( __dirname , "../samples/demo/index .html" ) ) ;
4647} ) ;
4748
4849app . get ( "/hello-world" , ( req , res ) => {
4950 res . sendFile ( path . join ( __dirname , "../samples/hello-world.html" ) ) ;
5051} ) ;
5152
53+ // Allow upload feature
54+ app . post ( "/upload" , function ( req , res ) {
55+ try {
56+ // Create a new Formidable form
57+ const form = formidable ( {
58+ multiples : false ,
59+ keepExtensions : true ,
60+ } ) ;
61+
62+ form . parse ( req , ( err , fields , files ) => {
63+ if ( err ) {
64+ console . error ( err ) ;
65+ return res . status ( 500 ) . send ( "Error processing the file upload." ) ;
66+ }
67+
68+ const uploadedFile = files . uploadFile [ 0 ] ; // Ensure the file field name matches the form
69+ if ( ! uploadedFile ) {
70+ return res . status ( 400 ) . json ( { success : false , message : "No file uploaded" } ) ;
71+ }
72+
73+ // Get current timestamp
74+ let dt = new Date ( ) ;
75+
76+ const fileSavePath = path . join ( __dirname , "\\" ) ;
77+ const newFileName = uploadedFile . originalFilename ;
78+ const newFilePath = path . join ( fileSavePath , newFileName ) ;
79+
80+ // Move the uploaded file to the desired directory
81+ fs . rename ( uploadedFile . filepath , newFilePath , ( err ) => {
82+ if ( err ) {
83+ console . error ( err ) ;
84+ return res . status ( 500 ) . send ( "Error saving the file." ) ;
85+ }
86+ console . log ( `\x1b[33m ${ newFileName } \x1b[0m uploaded successfully!` ) ;
87+ } ) ;
88+ res . status ( 200 ) . json ( {
89+ success : true ,
90+ message : `${ newFileName } uploaded successfully` ,
91+ filename : newFileName ,
92+ } ) ;
93+ } ) ;
94+ } catch ( error ) {
95+ res . status ( 500 ) . send ( "An error occurred during file upload." ) ;
96+ }
97+ } ) ;
98+
5299let httpPort = 3000 ;
53100let httpsPort = 3001 ;
54101
@@ -107,10 +154,11 @@ httpsServer.on("error", (error) => {
107154
108155// Start the servers
109156httpServer . listen ( httpPort , ( ) => {
110- console . log ( "\n\x1b[1m Dynamsoft Document Scanner Sample \x1b[0m\n" ) ;
111- console . log ( "\x1b[36m Access URLs:\x1b[0m" ) ;
157+ console . log ( "\n\x1b[1m Dynamsoft Document Scanner Samples \x1b[0m\n" ) ;
158+ console . log ( "\x1b[36m HTTP URLs:\x1b[0m" ) ;
112159 console . log ( "\x1b[90m-------------------\x1b[0m" ) ;
113- console . log ( "\x1b[32m Local:\x1b[0m http://localhost:" + httpPort + "/" ) ;
160+ console . log ( "\x1b[33m Hello World:\x1b[0m http://localhost:" + httpPort + "/hello-world" ) ;
161+ console . log ( "\x1b[33m Demo:\x1b[0m http://localhost:" + httpPort + "/demo" ) ;
114162} ) ;
115163
116164httpsServer . listen ( httpsPort , "0.0.0.0" , ( ) => {
@@ -124,13 +172,13 @@ httpsServer.listen(httpsPort, "0.0.0.0", () => {
124172 } ) ;
125173 } ) ;
126174
175+ console . log ( "\n" ) ;
176+ console . log ( "\x1b[36m HTTPS URLs:\x1b[0m" ) ;
177+ console . log ( "\x1b[90m-------------------\x1b[0m" ) ;
127178 ipv4Addresses . forEach ( ( localIP ) => {
128- console . log ( "\x1b[32m Network:\x1b[0m https://" + localIP + ":" + httpsPort + "/" ) ;
179+ console . log ( "\x1b[32m Hello World:\x1b[0m https://" + localIP + ":" + httpsPort + "/hello-world" ) ;
180+ console . log ( "\x1b[32m Demo:\x1b[0m https://" + localIP + ":" + httpsPort + "/demo" ) ;
129181 } ) ;
130- console . log ( "\x1b[36m Available Pages:\x1b[0m" ) ;
131- console . log ( "\x1b[90m-------------------\x1b[0m" ) ;
132- console . log ( "\x1b[33m Demo:\x1b[0m /demo" ) ;
133- console . log ( "\x1b[33m Hello World:\x1b[0m /hello-world\n" ) ;
134-
182+ console . log ( "\n" ) ;
135183 console . log ( "\x1b[90mPress Ctrl+C to stop the server\x1b[0m\n" ) ;
136184} ) ;
0 commit comments