Express - Serve Static Files

anonymous·2021년 9월 11일
0

How to serve static files?

  • You can use the express.static middleware to serve static files, including your images, CSS and JavaScript
  • static() is the only middleware function that is actually part of Express

Hello Static Files

1 Add Static Files in public DIR.

// serve images, CSS files, and JavaScript files from a directory named 'public' 
//Any files in the public directory are served by adding their filename (relative to the base "public" directory) to the base URL
app.use(express.static('public'));

2 Serve Static files in multiple DIRs.

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

3 create a virtual prefix for your static URLs, rather than having the files added to the base URL.

app.use('/media', express.static('public'));

Result

http://localhost:3000/media/images/dog.jpg
http://localhost:3000/media/video/cat.mp4
http://localhost:3000/media/cry.mp3

REF

https://developer.mozilla.org/en-US/docs/Learn/Server-side/Express_Nodejs/Introduction

profile
기술블로거입니다

0개의 댓글