1+ import path from "path" ;
2+ import fs from "fs" ;
13import express from "express" ;
24import cors from "cors" ;
35
@@ -7,27 +9,39 @@ const PORT = process.env.PORT ? parseInt(process.env.PORT, 10) : 4000;
79app . use ( cors ( { origin : true } ) ) ;
810app . use ( express . json ( ) ) ;
911
10- // Simple health endpoint
11- app . get ( "/health" , ( _req , res ) => {
12- res . json ( { status : "ok" , uptime : process . uptime ( ) } ) ;
13- } ) ;
14-
15- // Mock auth endpoints for the editor
16- let mockUser = {
12+ // Mock user for auth endpoints
13+ const mockUser = {
1714 id : "user-1" ,
1815 name : "Demo User" ,
19- avatar : "https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp&s=128"
16+ avatar :
17+ "https://www.gravatar.com/avatar/00000000000000000000000000000000?d=mp&s=128" ,
2018} ;
2119
22- app . get ( "/me" , ( _req , res ) => {
20+ // API mounted under /api to make it easy to host frontend and backend together
21+ app . get ( "/api/health" , ( _req , res ) => {
22+ res . json ( { status : "ok" , uptime : process . uptime ( ) } ) ;
23+ } ) ;
24+
25+ app . get ( "/api/me" , ( _req , res ) => {
2326 res . json ( { authenticated : true , profile : mockUser } ) ;
2427} ) ;
2528
26- app . post ( "/logout" , ( _req , res ) => {
27- // for a mock server just return success
29+ app . post ( "/api/logout" , ( _req , res ) => {
2830 res . json ( { ok : true } ) ;
2931} ) ;
3032
33+ // Serve static frontend if present in the final image at '../editor-dist'
34+ const staticPath = path . join ( __dirname , ".." , "editor-dist" ) ;
35+ if ( fs . existsSync ( staticPath ) ) {
36+ app . use ( express . static ( staticPath ) ) ;
37+
38+ // SPA fallback: any non-API route should return index.html so the client-side router can handle it
39+ app . get ( "/*" , ( req , res , next ) => {
40+ if ( req . path . startsWith ( "/api/" ) ) return next ( ) ;
41+ res . sendFile ( path . join ( staticPath , "index.html" ) ) ;
42+ } ) ;
43+ }
44+
3145app . listen ( PORT , ( ) => {
3246 console . log ( `Server running on http://localhost:${ PORT } ` ) ;
3347} ) ;
0 commit comments