NodeJS:如何获得绝对路径 - 作为'路径'不管用

时间:2017-09-12 22:11:18

标签: javascript node.js macos path

在我的nodeJS应用程序(meteorJS)中,我需要使用绝对路径通过fs.createWriteStream创建文件:

const absolutePath = '/Users/Anybody/Documents/Project/imports/temp'
if (Meteor.isServer) {
  const result = new Promise(function (resolve, reject) {
    const gfs = Grid(
      MongoInternals.defaultRemoteCollectionDriver().mongo.db,
      MongoInternals.NpmModule
    )
    const readerStream = gfs.createReadStream({ _id: id })
    const writerStream = fs.createWriteStream(absolutePath + 'temp.mp4')
    // ...
  })
}

这就是我在macOS上进行开发的代码。应用程序的生产版本部署到ubuntu服务器。 所以绝对路径是不同的。

我尝试使用path

const path = require('path')
const absolutePath = path.resolve('/imports/temp')

但是这只给了我/imports/temp(在macOS上)

1 个答案:

答案 0 :(得分:0)

您可以使用process.env.PWD获取项目的基本路径。然后,您可以添加剩余的文件夹段路径。

var base = process.env.PWD;
const absolutePath = base + '/imports/temp';
相关问题