serve public folder express without file extension 1

serve public folder express without file extension

// projectDirectory/src/index.js
const path = require('path')
const publicDirectoryPath = path.join(__dirname, '../public')
app.use(express.static(publicDirectoryPath))

// projectDirectory/public -> create index.html
// localhost:3000/index.html -> Here you go..

Here is what the above code is Doing:
1. We are using the express.static middleware to serve up the public directory.
2. We are using the path module to join the current directory name with the public directory.
3. We are using the app.use() function to use the middleware.
4. We are using the path.join() function to join the current directory name with the public directory.
5. We are using the __dirname global variable to get the current directory name.
6. We are using the app.use() function to use the middleware.
7. We are using the express.static() function to serve up the public directory.
8. We are using the path.join() function to join the current directory name with the public directory.
9. We are using the __dirname global variable to get the current directory name.

Similar Posts