Setting up two different static directories in node.js Express framework

后端 未结 6 703
北恋
北恋 2020-11-30 22:21

Is it possible? I would like to set up two different directories to serve static files. Let\'s say /public and /mnt

6条回答
  •  时光取名叫无心
    2020-11-30 22:39

    const express = require('express');
    const path = require('path');
    const pagesPath = path.join(__dirname, '/cheatsheet');
    const cssPath = path.join(__dirname, '/stylesheet');
    const port = process.env.PORT || 3000;
    
    var app = express();
    
    app.use("/cheatsheet" ,express.static(pagesPath));
    app.use("/stylesheet",express.static(cssPath)); 
    
    app.get('/',(request,response)=>{
        response.send('Hello CSS!!!');
      });
    
    app.get('/bad',(request,response)=>{
    response.send({error: 'Bad Request'});
    
    });
    app.listen(port, ()=> {
    console.log(`Server is running on Port ${port}` );
    console.log(__dirname);
    

    });

    // folder structure
    /cheatsheet/index.html
    /stylesheet/style.css
    

提交回复
热议问题