CSCI 3342 Web Development
Fall 2024

Session and Security

Model(-View-Controller)

Better code organization

So far, we have been putting all our code in our server files. Like with any project in any language, this is not realistic. Web applications (and frameworks) often follow the Model-View-Controller (MVC) software pattern to organize code:

  • The Models contain the data and logic for the system
  • The Views are the front-end display
  • The Controllers responds to user actions using the Models and Views

In our web apps, the server routes are the Controllers and the EJS templates are the Views. Now we will create our first model, a User class. Continuing in the same working directory, start testing.js with node.

Open a browser at http://localhost:8081 and you should see this test page. This page will allow you to write the user model by passing one test at a time.

Open the database file awesome.sqlite in DB Browser for SQLite so that you can see the data you're working with.

Open testing.js and models/user.js and follow along the video below to get started.

Creating a User Model With a JavaScript Class (10:43)

Video from a prior semester, no significant differences.

Complete models/user.js so that all the tests in testing.js pass, as shown in the image below.

Note that the test to add a new user will only work once. After that, it will fail and indicate that the username is a duplicate. This is fine. You can manually delete the added rows with DB Browser for SQLite as you test. (Don't forget to "Write Changes"!)

Assignment Checklist

Complete models/user.js so that all the tests in testing.js pass