将数据库对象附加/扩展到Restify服务器或请求

时间:2016-09-21 15:11:15

标签: node.js restify

使用可在每条路线中使用的restify对象扩展db的最佳做法是什么?

理想情况下,我想做类似于express练习的事情。

const app = express();

app.use('/api/users', usersRestApi);

app.get('/', (req, res) => {
  res.send('Hello World');
});

// Create MongoDb connection pool and start the application
// after the database connection is ready
MongoClient.connect(config.database.url, { promiseLibrary: Promise }, (err, db) => {
  if (err) {
    logger.warn(`Failed to connect to the database. ${err.stack}`);
  }
  app.locals.db = db;
  app.listen(config.port, () => {
    logger.info(`Node.js app is listening at http://localhost:${config.port}`);
  });
});

0 个答案:

没有答案