2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
id: 589690e6f9fc0f352b528e6e
|
2021-03-22 13:52:28 +00:00
|
|
|
|
title: 使用模块清理项目
|
2018-10-10 22:03:03 +00:00
|
|
|
|
challengeType: 2
|
2020-09-17 10:49:58 +00:00
|
|
|
|
forumTopicId: 301549
|
2021-01-13 02:31:00 +00:00
|
|
|
|
dashedName: clean-up-your-project-with-modules
|
2018-10-10 22:03:03 +00:00
|
|
|
|
---
|
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --description--
|
2020-09-17 10:49:58 +00:00
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
|
目前,我们把所有的代码都放到了 `server.js` 文件里, 这会导致代码难以维护,且扩展性差。 现在让我们来创建两个新文件:`routes.js` 和 `auth.js`。
|
2021-02-06 04:42:36 +00:00
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
|
在每个文件的开头,我们都需要写上这段代码:
|
2020-09-17 10:49:58 +00:00
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
module.exports = function (app, myDataBase) {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
```
|
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
|
然后,在 server.js 文件的开头,像这样引入文件:`const routes = require('./routes.js');`。在成功连接数据库之后,像这样进行实例化:`routes(app, myDataBase)`。
|
2020-09-17 10:49:58 +00:00
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
|
最后,把所有路由相关的代码从 server.js 移动到新文件。 不要忘了,`ensureAuthenticated` 方法的定义也要移动到新文件中,这个是我们在之前的挑战中,为在路由中判断用户是否已登录创建的函数。 然后,在 `routes.js`文件开头添加所需要的依赖,如:`const passport = require('passport');`。
|
2020-09-17 10:49:58 +00:00
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
|
如果在这些步骤后没有报错,那么你已成功地从 server.js 文件中分离出了路由文件(**除了 catch block 中的路由**)!
|
2020-09-17 10:49:58 +00:00
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
|
现在,我们来把 server.js 中与验证相关的代码分离到 auth.js 中,例如序列化,设置验证策略等。 请正确添加依赖,并在 server.js 中调用 `auth(app, myDataBase)`。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
|
完成上述要求后,请提交你的页面链接。 如果你遇到了问题,可以参考[这里](https://gist.github.com/camperbot/2d06ac5c7d850d8cf073d2c2c794cc92)的答案。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --hints--
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
2021-03-22 13:52:28 +00:00
|
|
|
|
应该有模块。
|
2018-10-10 22:03:03 +00:00
|
|
|
|
|
|
|
|
|
```js
|
2020-12-16 07:37:30 +00:00
|
|
|
|
(getUserInput) =>
|
|
|
|
|
$.get(getUserInput('url') + '/_api/server.js').then(
|
|
|
|
|
(data) => {
|
|
|
|
|
assert.match(
|
|
|
|
|
data,
|
|
|
|
|
/require\s*\(('|")\.\/routes(\.js)?\1\)/gi,
|
|
|
|
|
'You should have required your new files'
|
|
|
|
|
);
|
|
|
|
|
assert.match(
|
|
|
|
|
data,
|
2021-04-28 13:13:20 +00:00
|
|
|
|
/client\s*\.db[^]*routes/gi,
|
2020-12-16 07:37:30 +00:00
|
|
|
|
'Your new modules should be called after your connection to the database'
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
(xhr) => {
|
|
|
|
|
throw new Error(xhr.statusText);
|
|
|
|
|
}
|
|
|
|
|
);
|
2018-10-10 22:03:03 +00:00
|
|
|
|
```
|
2020-08-13 15:24:35 +00:00
|
|
|
|
|
2020-12-16 07:37:30 +00:00
|
|
|
|
# --solutions--
|
|
|
|
|
|
2021-01-13 02:31:00 +00:00
|
|
|
|
```js
|
|
|
|
|
/**
|
|
|
|
|
Backend challenges don't need solutions,
|
|
|
|
|
because they would need to be tested against a full working project.
|
|
|
|
|
Please check our contributing guidelines to learn more.
|
|
|
|
|
*/
|
|
|
|
|
```
|