3.1 KiB
3.1 KiB
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
5895f70cf9fc0f352b528e67 | Implement the Serialization of a Passport User | 2 | 实现Passport用户的序列化 |
Description
const mongo = require('mongodb').MongoClient;
)现在我们想要连接到我们的数据库,然后开始侦听请求。这样做的目的是在连接数据库之前或者出现数据库错误时不允许请求。要实现此目的,您需要在以下内容中包含序列化和应用程序侦听器: mongo.connect(process.env.DATABASE,(err,db)=> { if(错误){ console.log('数据库错误:'+错误); } else { console.log('成功的数据库连接');您现在可以在deserializeUser中取消注释该块并删除您的//serialization and app.listen
}});
done(null, null)
。确保将.env文件中的DATABASE设置为数据库的连接字符串(例如: DATABASE=mongodb://admin:pass@mlab.com:12345/my-project
)。您可以在mLab上设置免费数据库。恭喜 - 您已完成序列化设置!当您认为自己已经做对时,请提交您的页面。如果您遇到错误,可以在这里查看到目前为止完成的项目。 Instructions
Tests
tests:
- text: 存在数据库连接
testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.match(data, /mongo.connect/gi, "You should have created a connection to your database"); assert.match(data, /mongo.connect[^]*app.listen[^]*}[^]*}/gi, "You should have your app.listen nested at within your database connection at the bottom"); }, xhr => { throw new Error(xhr.statusText); })'
- text: '反序列化现在正确使用DB并且擦除了<code>done(null, null)</code>'
testString: 'getUserInput => $.get(getUserInput("url")+ "/_api/server.js") .then(data => { assert.notMatch(data, /null,( |)null/gi, "The callback in deserializeUser of (null, null) should be completely removed for the db block uncommented out"); }, xhr => { throw new Error(xhr.statusText); })'
Challenge Seed
Solution
// solution required