This assignment will allow to practise building the database structure for a registration form
This project will allow you to practise;
- Connecting to a MongoDB database
- Writing a schema for a MongoDB database with Mongoose
Note: For this assignment, we already assume you have a MongoDB server, and know how to access your credentials.
- Using the
.env.example
file as a template, create a.env
file
The database connection string should be provided to you by MongoDB
-
Install the dotenv npm package
-
Import
dotenv
intoserver.js
-
Add the following code to parse your
.env
variables into theprocess.env
global object:dotenv.config();
We will install mongoose
and connect it to our database
-
Install the mongoose npm package
-
Import
mongoose
intoserver.js
-
Use the
connect
method frommongoose
to connect to your database, using the connection string you can now read with theprocess.env
global variablemongoose.connect();
The mongoose.connect()
method returns a promise, which we can use to determine if the connection with the database worked or not.
-
Use the
then
method (the promise was resolved) to display aconsole.log()
message that the connection was successful -
Use the
catch
method (the promise was rejected) to display aconsole.log()
message that the database connection failed -
Test your code by running it. What message do you see in printed in the
console
?
Now we've connected our database, we want to build an endpoint which will allow us to register new users onto our website. To do this, we will have to use express.js
-
Install the express npm package
-
Import
express
intoserver.js
-
Make a call to
app.listen()
to allow the server to start listening for incoming connections. We will use port3001
.Bonus: Move the port number into your
.env
file -
Create an endpoint. It should:
- listen for a
POST
request (we want to receive data) - use the path
/register
- don't forget
response.send()
!
- listen for a
-
Use an API testing tool such as Postman or Insomnia to test your endpoint.
If your endpoint works, move onto the next assignment
In the file models/User.js
;
-
Import
mongoose
intomodels/User.js
-
Create a schema and assign it to the variable
userSchema
. The schema should have the following fields;username password firstName lastName dateOfBirth email telephone gender
dateOfBirth
should be of typeDate
The rest should be of type
String
-
username
,password
andemail
should berequired
-
gender
should have anenum
validation. It should accept only the strings:'Male'
'Female'
'Other'
'N/A'
-
gender
should default to the string'N/A'
Now our schema has been defined, we must instantiate it into a model
By doing this it will give us access to the document methods such as
create()
, allowing us to quickly and easily save data into our database.
In the file models/User.js
;
- Create model from the
userSchema
and assign it to a variable calledUser
- Export the variable
User
frommodels/User.js
- Import the
User
model intoserver.js
We will replace our sample data with the data we receive from the POST request
Before we can do this, we must prepare our application to receive data.
-
Install the cors npm package
-
Add the middleware
express.json()
andcors()
to your serverHint: Don't forget to import
cors
before trying to use it
When we save data to our model with the create()
method, the data is automatically saved to the database
Our data for the new user will come from the request body object - for this step we will assume that all the properties on this object match the fields in the User
schema
Inside the /register
endpoint;
-
use the
User
model with thecreate()
method to save a user into the databaseUser.create({});
create()
returns aPromise
! Either use theasync / await
keywords orthen()
andcatch()
methods -
Fill in the fields with the properties from the request body object
-
Update your code to handle errors
- If the
Promise
fails send a status code of400
with an appropriate message - If the
Promise
succeeds send a status code of200
with an appropriate message
Hint: You will need 2
response.send()
statementsHint: You can send a different response code with
response.status()
- If the
-
Test your endpoint. Use a MongoDB database viewing tool such as Compass to check if the data you added is in the database.
-
Create an endpoint inside
server.js
. It should:- listen for a
GET
request (we want to receive data from the server) - use the path
/list
- don't forget
response.send()
- listen for a
-
Inside the handler for the
/list
endpoint, use the methodUser.find()
to get all the users saved in the databaseUser.find()
returns aPromise
- Return the results to the user
Build a frontend which will make the POST
requests you were previously testing with your API testing tool.
-
The frontend should consist of a
<form>
, which will take the following details:- username
- password
- firstName
- lastName
- dateOfBirth
- telephone
- gender
There should be a button to
Submit
the form