我不知道如何向我的server.js添加SSL证书,因此我可以通过https访问服务器上的API。
var express = require('express'),
cors = require('cors'),
app = express(),
port = process.env.PORT || 3000,
mongoose = require('mongoose'),
Task = require('./api/models/todoListModel'), //created model loading here
bodyParser = require('body-parser'),
helmet = require('helmet');
// Test SSL connection
var MongoClient = require('mongodb').MongoClient;
// mongoose instance connection url connection
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/Tododb'); // was tododb
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// adding Helmet to enhance your API's security
app.use(helmet());
// enabling CORS for all requests
app.use(cors());
app.get('/', (req, res) => res.send('Hello World!'))
var routes = require('./api/routes/todoListRoutes'); //importing route
routes(app); //register the route
app.listen(port);
console.log('Supporter RESTful API server started on: ' + port);
我已经尝试过mongoose.connect('mongodb://localhost/Tododb&ssl=true');
,但是我真的不知道该怎么办?我知道我必须添加对我生成的密钥和证书文件的引用,但是我不知道如何将它们添加到连接字符串中。
我一直在尝试遵循其中的一些https://docs.mongodb.com/manual/reference/connection-string/
下一步是什么?
下一步是向mongoose.connect调用提供ConnectionOptions。猫鼬文档(位于https://mongoosejs.com/docs/connections.html#options)指定它将ssl特定的选项传递给基础MongoClient。可以在以下位置找到MongoClient的选项:http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect
您所困扰的对象可能是sslCert,sslKey,甚至是sslPass(以及更多的ssl *设置)。请注意,我在此处列出的内容可能需要您自己在代码中加载文件。您可能可以使用类似:
var fs = require('fs');
var options = {
...
sslCert: fs.readFileSync(path.join(__dirname, <relative path to the cert>))
};
mongoose.connect('mongodb://localhost/Tododb', options);