2020-04-04 10:36:08 +03:00
const path = require ( 'path' ) ;
const express = require ( 'express' ) ;
2021-04-25 23:03:55 +03:00
const reload = require ( 'reload' ) ;
2020-04-04 10:36:08 +03:00
const app = express ( ) ;
const BUILDDIR = process . argv [ 2 ] || './build' ;
const PORT = process . argv [ 3 ] || 3000 ;
2022-01-13 22:40:35 +03:00
const FULLBUILDPATH = path . join ( _ _dirname , BUILDDIR )
2020-04-04 10:36:08 +03:00
2022-01-13 22:40:35 +03:00
//LOGGING
app . use ( ( req , res , next ) => {
2022-02-25 17:49:30 +03:00
//////console.log('REQUEST', req.method+' '+req.originalUrl, res.statusCode);
2022-01-13 22:40:35 +03:00
next ( ) ;
} ) ;
//apply to every request
app . use ( ( req , res , next ) => {
//disable caching
res . set ( "Cache-Control" , "no-store" ) ;
//enabled/disable reload module
if ( process . env . RELOAD === "yes" ) res . locals . reload = true ;
return next ( ) ;
} ) ;
2021-04-29 00:33:53 +03:00
2020-04-04 10:36:08 +03:00
//ROUTE - other files
2022-01-13 22:40:35 +03:00
app . use ( '/' , express . static ( FULLBUILDPATH , {
//custom function required for logging static files
setHeaders : ( res , filePath , fileStats ) => {
2022-02-25 17:49:30 +03:00
//console.info('GET', '/'+path.relative(FULLBUILDPATH, filePath), res.statusCode);
2022-01-13 22:40:35 +03:00
}
} ) ) ;
//ROUTE - match / or any route with just numbers letters and dashes, and return index.htm (all other routes should have been handled already)
app . get ( '/' , ( req , res , next ) => {
2022-02-25 17:49:30 +03:00
////console.log('root')
2022-02-23 19:16:23 +03:00
res . sendFile ( path . join ( _ _dirname , BUILDDIR , 'index.htm' ) , { } , function ( err ) {
2022-02-25 17:49:30 +03:00
////console.log('sent file');
2022-02-23 19:16:23 +03:00
return next ( ) ;
} ) ;
} ) ;
app . get ( '/pixel-editor' , ( req , res , next ) => {
2022-02-25 17:49:30 +03:00
////console.log('root')
2022-02-23 19:16:23 +03:00
res . sendFile ( path . join ( _ _dirname , BUILDDIR , 'index.htm' ) , { } , function ( err ) {
2022-02-25 17:49:30 +03:00
////console.log('sent file');
2022-02-23 19:16:23 +03:00
return next ( ) ;
} ) ;
} ) ;
app . get ( '/pixel-editor/?:palette/?:resolution/?:patternWidth/?:patternBinStr' , ( req , res , next ) => {
2022-02-25 17:49:30 +03:00
////console.log('root')
2022-01-13 22:40:35 +03:00
res . sendFile ( path . join ( _ _dirname , BUILDDIR , 'index.htm' ) , { } , function ( err ) {
2022-02-25 17:49:30 +03:00
////console.log('sent file');
2022-01-13 22:40:35 +03:00
return next ( ) ;
} ) ;
} ) ;
2020-04-04 10:36:08 +03:00
2022-01-13 22:40:35 +03:00
//HOT RELOADING
2021-04-29 00:36:41 +03:00
// "reload" module allows us to trigger webpage reload automatically on file changes, but inside pixel editor it also
// makes browser steal focus from any other window in order to ask user about unsaved changes. It might be quite
// intrusive so we decided to give option to choose preferred workflow.
if ( process . env . RELOAD === "yes" ) {
reload ( app ) . then ( ( ) => {
//start server
app . server = app . listen ( PORT , ( ) => {
2022-02-25 17:49:30 +03:00
////console.log(`Web server listening on port ${PORT} (with reload module)`);
2022-01-13 22:40:35 +03:00
2021-04-29 00:36:41 +03:00
} )
} ) ;
} else {
app . listen ( PORT , ( ) => {
2022-02-25 17:49:30 +03:00
////console.log(`Web server listening on port ${PORT}`);
2021-04-25 23:03:55 +03:00
} )
2021-04-29 00:36:41 +03:00
}
2021-07-07 00:24:20 +03:00
2022-01-13 22:40:35 +03:00
app . use ( function ( req , res , next ) {
res . status ( 404 ) ;
res . type ( 'txt' ) . send ( 'The requested resource does not exist. Did you spell it right? Did you remember to build the app? It\'s probably your fault somehow.' ) ;
return next ( ) ;
2021-07-07 00:24:20 +03:00
} ) ;
2022-01-13 22:40:35 +03:00
//LOGGING
app . use ( ( req , res , next ) => {
2022-02-25 17:49:30 +03:00
////console.log(req.method+' '+req.originalUrl, res.statusCode);
2022-01-13 22:40:35 +03:00
} ) ;