This project is designed as a starting point for the NodeJS workshop aimed at introducing participants to building and testing APIs using NodeJS, Express, and external API integration. By the end of this workshop, participants will have a basic API that can query external services and serve data, which can be tested using tools like Insomnia or Postman.
Before you begin, ensure you have installed the following:
- Node.js: Download and install Node.js, including npm (Node Package Manager) which comes with node. Use the LTS (Long Term Support).
- Git: Download and install Git. This is necessary for cloning the starter code repository from GitHub.
- Code Editor: Any code editor of your choice, but we recommend Visual Studio Code for its extensive support for JavaScript and Node.js development.
- Postman or Insomnia: These are tools for testing APIs. You can choose either:
- Knowledge: Please read up on basic HTTP request methods and HTTP response codes.
-
Clone (or fork) the Repository
- Open a terminal or command prompt.
- Run
git clone https://github.com/fygure/NodeJS-API-Workshop-1.git
- Navigate into the project directory:
cd NodeJS-API-Workshop-1
-
Install Dependencies
- Inside the project directory, run
npm install
to install the required npm packages. - This reads the dependencies from the package.json file and installs them into a node_modules folder.
- Inside the project directory, run
-
Environment Configuration
- If your project requires environment variables, add a
.env
file to the root of your project and populate it as necessary (e.g.,PORT=3001
).
- If your project requires environment variables, add a
-
Change the Port Number (if necessary)
- Open the
server.js
file in your code editor. - Locate the line where the port number is defined, e.g.,
const PORT = process.env.PORT || 3001;
. - Change
3001
to the unique port number assigned to you for this workshop.
- Open the
-
Start the Server
- Run
npm start
in the terminal from the root directory of your project. - If everything is set up correctly, you should see a message like
Server is running on port XXXX
, indicating the server has started.
- Run
To test your API, you can use Postman or Insomnia:
- Launch Postman/Insomnia.
- Create a new request to test the endpoints you've developed. For example, if you have a GET endpoint at
/api/data
, you would send a GET request tohttp://localhost:XXXX/api/data
, replacingXXXX
with your unique port number. - When naming routes, best practice is to include a version number such as
/api/v1/<route_name>
.
- Introduction to NodeJS and APIs
- Setting Up the Environment
- Understanding the Starter Code
- Building a Simple API with Express
- Querying an External API
- Avoiding Port Conflicts
- Testing the API with Insomnia or Postman
- Debugging and Troubleshooting
- Port Already in Use: Ensure the port number in your
.env
file orserver.js
is unique. - Dependencies Not Found: Run
npm install
from the root of your project directory to install missing npm packages.
- Node.js Official Documentation
- Express.js Documentation
- Postman Learning Center
- Insomnia Documentation
- Public APIs
- Chrome JSON Viewer Extension
If you want to set this up yourself first ensure that Node.js and npm are installed. Then you can set up your project folder and install the necessary dependencies.
Then you will have to create each file manually such as an server.js
file, but be sure to update the package.json entry point. The package.json file also contains the scripts that you can use or create yourself.
mkdir node-server
Make folder.cd node-server
Navigate terminal to folder.npm init
Creates the package.json file (appending-y
will use defaults).npm install express
Installs express as a dependency in your package.json file, but you can replaceexpress
with another npm package.
If you have any questions or encounter issues, please reach out to the Max or other CougarCS Mentors after the workshop during Q&A time.