账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    nodejs 实现生产环境或开发环境引用对应js/css/img路径
    44
    0

    nodejs 如何实现假如是开发环境引用的路径是dist/css/xx.css

            生产环境引用的路径是build/css/xx.css

    项目使用的是ejs模板 express框架
    这样子的需求应如何实现 谢谢

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • Ю↘珂ル° 普通会员 1楼

      在Node.js中,你可以使用fs模块来读取文件路径。但是,如果你需要在生产环境中或开发环境中引用不同类型的文件,如js/css/img,你可以使用一些库,如path,browserify,rollup等。

      以下是一个使用path库的简单示例:

      ```javascript const path = require('path');

      // 在生产环境中 const productionPath = path.resolve(__dirname, 'dist');

      // 在开发环境中 const developmentPath = path.resolve(__dirname, 'build');

      // 遍历生产环境的文件 for (const filePath of productionPath) { // 如果文件是js文件,打印文件路径 if (path.extname(filePath) === '.js') { console.log(filePath); } }

      // 遍历开发环境的文件 for (const filePath of developmentPath) { // 如果文件是css文件,打印文件路径 if (path.extname(filePath) === '.css') { console.log(filePath); } }

      // 遍历生产环境和开发环境的文件 for (const filePath of productionPath + developmentPath) { // 如果文件是js/css/img文件,打印文件路径 if (path.extname(filePath) === '.js/css/img') { console.log(filePath); } } ```

      在这个示例中,我们首先定义了生产环境和开发环境的文件路径。然后,我们使用for循环来遍历这两个路径,并打印出每个文件的路径。最后,我们使用一个for循环来遍历生产环境和开发环境的文件,如果文件是js/css/img文件,我们就打印出它的路径。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部