You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importexpressfrom'express';importHyperExpressfrom'hyper-express';importLiveDirectoryfrom'live-directory';importpathfrom'path';importmimefrom'mime-types';constUSE_HYPER_EXPRESS=false;constapp=USE_HYPER_EXPRESS ? newHyperExpress.Server() : express();constassets=newLiveDirectory('public',{static: process.env.NODE_ENV==='production',cache: {max_file_count: 100,max_file_size: 1024*1024*2.5,}});functionserveStaticFile(req,res,assets){letp=req.path;if(!req.path.includes('.')){p=path.join(req.path,'index.html').replaceAll('\\','/');// for some reason, live-directory doesn't seem to support Windows paths}constasset=assets.get(p);if(!asset){returnres.status(404).send(`Static file '${p}' not found`);}res.header('Content-Type',mime.lookup(p)??'application/octet-stream');if(asset.cached){returnres.send(asset.content);}else{constreadable=asset.stream();returnreadable.pipe(res);}}app.get('/',(req,res)=>{res.send('Hello World!');});app.get('/:name',(req,res,next)=>{if(req.params.name==='cat.jpg'){returnnext();}res.send(`Hello ${req.params.name}!`);});app.use((req,res)=>{serveStaticFile(req,res,assets);});// another inconsistency with express, would be nice to support callbacks when they're suppliedif(USE_HYPER_EXPRESS){app.listen(3001).then(()=>{console.log('Listening on port 3001');}).catch((err)=>{console.error(err);});}else{app.listen(3001,()=>{console.log('Listening on port 3001');});}
In this code, you can set USE_HYPER_EXPRESS to either true or false whether if you want to use hyper-express or not.
There's an endpoint /:name that behaves differently.
If you use express:
If you go to localhost:3001/test it will return Hello test!
If you go to localhost:3001/cat.jpg it will return the cat jpg inside public folder
If you use hyper-express:
If you go to localhost:3001/test it will return Static file '/test/index.html' not found, completely ignoring .get('/:name') endpoint
If you go to localhost:3001/cat.jpg it will return the cat jpg inside public folder
The implementation for chained routes from #198 should potentially resolve this issue and supporting callbacks would certainly reduce the incompatibility gap.
Are you aware of any other methods which hyper-express currently only offers as Promises but should support callbacks for better compatibility with Express?
So changing the routing behavior in the current version would be a breaking change for the current version and users. So the routing behavior consistency will be implemented in the next major v7.0 update which is currently in the works. I'll be sure to follow up once that is released to ensure all concerns are handled.
Consider this code:
In this code, you can set
USE_HYPER_EXPRESS
to eithertrue
orfalse
whether if you want to use hyper-express or not.There's an endpoint
/:name
that behaves differently.If you use
express
:localhost:3001/test
it will returnHello test!
localhost:3001/cat.jpg
it will return the cat jpg insidepublic
folderIf you use
hyper-express
:localhost:3001/test
it will returnStatic file '/test/index.html' not found
, completely ignoring.get('/:name')
endpointlocalhost:3001/cat.jpg
it will return the cat jpg insidepublic
folderHere's zip with code and files:
hyperexpress.zip
The text was updated successfully, but these errors were encountered: