我正在使用node.js + express构建应用程序,使用postgresql构建数据库,使用mocha + chai进行测试。我正在尝试测试进行数据库查询的API。但是,当我运行测试时,我不断收到错误:
{ [Error: ENOENT: no such file or directory, open '.env'] errno: -2, code: 'ENOENT', syscall: 'open', path: '.env' }
Knex:Error Pool2 - error: password authentication failed for user "m11111"
error: Error: Pool was destroyed
这是我的测试文件:
require('dotenv').load();
var chai = require('chai');
var chaiHttp = require('chai-http');
var server = require('../../app');
var should = chai.should();
var knex = require('../../models/database').knex;
chai.use(chaiHttp);
it('should get all projects', function(done) {
chai.request(server)
.get('/api/projects')
.end(function(err, res){
res.should.have.status(200);
res.should.be.json;
res.body.should.be.a('array');
done();
});
});
我正在测试的API:
api.get('/api/projects', function(req, res) {
console.log('get projects hit'); //this console logs out
knex('projects').select()
.then(function(data) {
res.send(data);
}).catch(function(error) {
console.log('error: ' + error);
res.sendStatus(200);
});
});
这是我的数据库连接字符串:
require('dotenv').load();
module.exports = {
knex: require('knex')({
client: 'pg',
connection: {
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_USER_PASSWORD,
database: process.env.DB_NAME,
port: process.env.DB_PORT
},
pool: {
min: 0,
max: 7
},
searchPath: process.env.DB_SEARCHPATH
})
};
有谁知道可能出错了什么?
提前致谢!