how to set up a dev environment for express.js
To begin, make a backend folder that will host your repository then create a git repo from here.
- npm init -y
- npm install express cors mongoose dotenv
from here we’ll have a lot of the tech that will be necessary to begin the development of a restful express backend API.
Here's a brief explanation of what each package does:
- express: a lightweight backend framework for node.js
- cors: cors stands for cross-origin resource sharing and this allows for Ajax requests to skip the same-origin policy and access resources from remote hosts. the cors package provides an express middleware that can allow cors with different options, this allows us to easily access something outside of our server from our server
- mongoose: make interacting with MongoDB trough node.js simpler
- dotENV: loads env variables through process.env
At this point you have access to what you need in order to get started working with the express, you just need the basic server.js file and require mongoose and express shown below
you’re then going to want to create a .env file and make a variable that is called ATLAS_URI and is assigned the URI given to you by MongoDB
make sure to change the password field to the password you chose when initializing your MongoDB server.
from here to test your server you can install another dependency called nodemon with:”>npm install nodemon” then running the command with
within your terminal.