Pets Back-End Setup
Setup
Open your Terminal application and navigate to your ~/code/ga/lectures directory:
cd ~/code/ga/lectures
Make a new directory called express-api-pets-back-end, then enter this directory:
mkdir express-api-pets-back-end
cd express-api-pets-back-end
Create server.js, .env, and .gitignore files:
touch server.js .env .gitignore
In the terminal, create a package.json with all of the default settings and install the required packages by running the following commands:
npm init -y
npm i express mongoose dotenv morgan
npm i -D nodemon
Open the contents of the directory in your code editor:
code .
Add the following boilerplate code to the server.js file:
const dotenv = require('dotenv');
dotenv.config();
const express = require('express');
const app = express();
const mongoose = require('mongoose');
const logger = require('morgan');
mongoose.connect(process.env.MONGODB_URI);
mongoose.connection.on('connected', () => {
console.log(`Connected to MongoDB ${mongoose.connection.name}.`);
});
app.use(express.json());
app.use(logger('dev'));
// Routes go here
app.listen(3000, () => {
console.log('The express app is ready!');
});
Add your MongoDB URI to the .env file:
MONGODB_URI=mongodb+srv://<username>:<password>@sei-w0kys.azure.mongodb.net/pets?retryWrites=true
If you are unsure of where to obtain your MongoDB URI, please refer to the MongoDB Atlas Setup Lab.
Start the server with nodemon
Finally, we are going to add a script to start our server in development mode using nodemon.
You should have already installed nodemon earlier using the following command:
npm i -D nodemon
The
-Dflag made this a development dependency. This makesnodemonavailable only during development.
Now we will add a quick script to our package.json.
Open your package.json file and locate the "scripts" section. Add a new script called "dev" to run your server with nodemon. It should look like this:
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node server.js",
"dev": "nodemon server.js"
},
startis for running the server inproductionmode.devis for running the server indevelopmentmode withnodemon.
To use nodemon to run your server, simply type the following command in your terminal:
npm run dev
This will start your server and automatically restart it whenever you make changes to your code.
Install Postman
We will be using Postman during this lesson to interact with our API. If you do not have Postman installed, please refer to the Postman Setup Lab.