Take a tour through a complete reservation Node.js Express application. Learn how to install its dependencies and start the server in debug mode.
- [Instructor] As this course is about code improvement, a complete application has been included as a starting point. Let's get it installed and take a tour of what it has to offer. Switch over to Atom. Navigate up to file, and select add project folder. I put my exercise files on the desktop and I want to select the folder named nadia. Click select folder, or open, depending on the platform. The project's been added to Atom, let's start with the code first.
The bin directory contains just one file, www.js. It's a standard express entry point that configures an http server, then executes the app. We're not going to be making any changes here. The lib directory contains our custom code. The middleware directory contains auth.js, which just configures the basic auth middleware. The schema directory contains a custom class that defines the structure of a reservation. It has a constructor, a helper to combine the date and time, and a validator method.
In the route of lib, is reservations.js. Which exports functions used for acting on reservations, including retrieval, creation, saving, and a validation wrapper. The migrations directory contains a sql light schema that will be automatically created when the application is started. The public directory contains the front end of the application. Contrib contains third party code, and the css and images and js folders are pretty self explanatory.
The routes directory contains three routes. Admin is the administrative interface, index is the homepage, and reservations is where the booking takes place. The last directory is views, which contains pug templates. If you're not familiar with it, pug is the successor to jade. This isn't a front end course, so we'll be leaving it alone. Finally, we've got app.js, which is the main express application. It's intentionally sloppy and has some inconsistencies right off the bat.
Let's get this project started. I'm going to enable the terminal by going up to packages, platformio-ide-terminal and selecting new terminal. First, let's tell npm that this is not a production environment. Type npm config set -g production false. I'm going to use npm to install everything we need by typing npm install. After a moment all dependencies will be installed.
We're going to use a debugging mode so we can see verbose output from the application. Type npm run debug. The application will bootstrap and we'll see the logs in real time. Open a browser and position it so you can see both the log file and the browser. Navigate to localhost:3000. This is the homepage for the restaurant. There's no functionality here except for a link to the reservations.
Let's click on reservations now. This is the booking request form and this is what we're going to be working on cleaning up. Let's book a reservation for today. Pick a time, and there's only one in my party. My name is Jon Peck, my email is going to be username. Notice that this is just username with no at domain. Because this is another bug in the application. Enter phone number 555 555 5555.
And then click the email icon to toggle the message and type hello, world! When you're ready, click request booking. Great, we've made a reservation even with a bad email address. Want to see it? Navigate to localhost:3000/admin. We'll be prompted with a basic auth. This is intentionally insecure. The user name and password are, admin, admin.
Click login. And the reservation booking request we made is shown. Let's clean up and shut down the server. Switch over back to the terminal, click on it to make sure you have focus, then press control c. If prompted to terminate a batch job if you're on windows, say yes. Let's close the terminal. Click the x in the upper corner, and right click on the tab, and select close all tabs. The good news is that it's functional, but we already know there are problems.
Ready to fix them? Fantastic, let's get started with no js testing and code quality.
- What is code quality?
- Testing and code quality fundamentals
- Coding conventions and standards
- Creating and enforcing coding standards
- Unit, integration, and functional testing
- Test-driven development test specificatons
- Behavior-driven development test specifications
- Finding errors with linting
- Extending an ESLint shareable config
- Validating correctness with unit testing
- Replacing and inspecting with stubs, spies, and mocks
- Code coverage and why it matters
- Coverage with continuous integration