Files
freeCodeCamp/curriculum/challenges/english/06-quality-assurance/advanced-node-and-express/clean-up-your-project-with-modules.english.md
Shaun Hamilton e3cfe80f88 fix(curriculum): advanced node express changes for new boilerplate (#39080)
* fix: add tests and steps

* add necessary changes

* edit for new boilerplate

* fix: adjust content for boilerplate merge

* add 4 passing 1 failing socketio

* fix: add socketio changes

* fix: update wording and http test

Co-authored-by: Kristofer Koishigawa <scissorsneedfoodtoo@gmail.com>

* fix: replace glitch remix urls with repl.it urls

* integrate steps between lessons 4 and 5

* add mongodb altas link

* edit test to not require db deletion

* correct register routing and formatting

* fix typos and formatting

* fix: typos, standardize spacing, and remove unnecessary hr elements

* fix: add/update links

Add or update Gist solution links at the bottom of each challenge. Also add a missing link/text to the top of one of the challenges.

* fix: remove Repl.it/boilerplate repo links from all but first challenge

* fix: add target='_blank' to links in challenges

* add note about PIP browser issues

* move PIP note to end of instructions

Co-authored-by: Kristofer Koishigawa <scissorsneedfoodtoo@gmail.com>
2020-09-04 08:50:03 -05:00

2.6 KiB

id, title, challengeType, forumTopicId
id title challengeType forumTopicId
589690e6f9fc0f352b528e6e Clean Up Your Project with Modules 2 301549

Description

Right now everything you have is in your server.js file. This can lead to hard to manage code that isn't very expandable. Create 2 new files: routes.js and auth.js

Both should start with the following code:

module.exports = function (app, myDataBase) {

}

Now in the top of your server file, require these files like such: const routes = require('./routes.js'); Right after you establish a successful connect with the database instantiate each of them like such: routes(app, myDataBase)

Finally, take all of the routes in your server and paste them into your new files and remove them from your server file. Also take the ensureAuthenticated since we created that middleware function for routing specifically. You will have to now correctly add the dependencies in that are used, such as const passport = require('passport');, at the very top above the export line in your routes.js file.

Keep adding them until no more errors exist, and your server file no longer has any routing (except for the route in the catch block)!

Now do the same thing in your auth.js file with all of the things related to authentication such as the serialization and the setting up of the local strategy and erase them from your server file. Be sure to add the dependencies in and call auth(app, myDataBase) in the server in the same spot.

Submit your page when you think you've got it right. If you're running into errors, you can check out an example of the completed project here.

Instructions

Tests

tests:
  - text: Modules should be present.
    testString: 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, /client.db[^]*routes/gi, 'Your new modules should be called after your connection to the database'); }, xhr => { throw new Error(xhr.statusText); })

Challenge Seed

Solution

/**
  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.
*/