Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why need use express static

Sorry for silly question but i really want to know why we need to use it . I know stactic file are files that aren't being generated as they're loaded, but must be sent to the browser when requested .

I would appreciate if anyone can explain for me these questions.

like image 670
Nguyễn Bảo Dũng Avatar asked Oct 12 '25 09:10

Nguyễn Bảo Dũng


1 Answers

Imagine a scenario where you are creating an API as well as want to serve your static assets (HTML, CSS, JavaScript, etc) from the same Express application.

In this case it makes a lot of sense to define a folder - normally it's called 'public' - and have all the previously mentioned static assets there, e.g.:

app.use(express.static('public'))

Now that's out of the way you can go and create your API using Node.JS/Express with a nice separation of what's a static asset and what's loaded dynamically by your app. At least this is how I use it. I hope this will help.

like image 71
Tamas Avatar answered Oct 14 '25 09:10

Tamas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!