FileSync找不到我的文件

时间:2015-04-14 12:15:09

标签: node.js ssl

FileySync找不到其他模块的子文件夹(我也尝试过相同的文件夹)中的文件。

Folder structure

console.log __dirname
https = require('https')
fs = require('fs')
sslOptions = {
  key: fs.readFileSync('./config/ssl/server.key'),
  cert: fs.readFileSync('./config/ssl/server.crt'),
  ca: fs.readFileSync('./config/ssl/ca.crt'),
  requestCert: true,
  rejectUnauthorized: false
}
secureServer = https.createServer(sslOptions,app).listen('3030', () ->
  console.log("Secure Express server listening on port 3030")
)

configDB = require('./config/database.js')(environment)

找到了database.js,ssl文件没有找到。记录的目录名称:

---my project folder--/.tmp/serve

文件权限:

config

-rw-r--r--  1 MasterG  staff   93 14 Apr 14:06 config.js
-rw-r--r--  1 MasterG  staff  526 14 Apr 14:06 database.js
drwxr-xr-x  6 MasterG  staff  204 14 Apr 13:51 ssl

SSL

-rw-r--r--  1 MasterG  staff  912 14 Apr 14:06 ca.crt
-rw-r--r--  1 MasterG  staff  963 14 Apr 14:06 ca.key
-rw-r--r--  1 MasterG  staff  924 14 Apr 14:06 server.crt
-rw-r--r--  1 MasterG  staff  887 14 Apr 14:06 server.key

错误: unction strWrapper(err,written){                 ^ 错误:

ENOENT, no such file or directory './config/server.key'
    at Error (native)
    at Object.fs.openSync (fs.js:502:18)
    at Object.fs.readFileSync (fs.js:354:15)
    at Object.<anonymous> (/Users/MasterG/Desktop/PROJEKTE/gideon/.tmp/serve/server.js:25:13)
    at Object.<anonymous> (/Users/MasterG/Desktop/PROJEKTE/gideon/.tmp/serve/server.js:76:4)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)

1 个答案:

答案 0 :(得分:4)

sslOptions = {
  key: fs.readFileSync(__dirname + '/config/ssl/server.key'),
  cert: fs.readFileSync(__dirname + '/config/ssl/server.crt'),
  ca: fs.readFileSync(__dirname + '/config/ssl/ca.crt'),
  requestCert: true,
  rejectUnauthorized: false
}

要包含__dirname就可以了,这很奇怪,因为在那个教程中,似乎没有: http://greengeckodesign.com/blog/2013/06/15/creating-an-ssl-certificate-for-node-dot-js/