-
Add nodemon to Docker so that docker doesn't require a start and stop every time.
-
Figure out where the console.log output is sent. I don't see it in the terminal when the API is running. This will also help with errors / issues since at the moment, if an image fails to be run as a Docker container, you have to look at the logs of the container versus seeing some message immediately in a console.
-
Run this set-up from a clean machine to identify any updates to the ReadMe since there may need to be some other Node install required.
-
Remove any unnecessary Node packages.
-
Clarify the name of in the Docker Troubleshooting section.
Added a docker container to begin preparing to add a database. I haven't figured out nodemon for Docker (ie your changes automatically refresh the server), but that will be added.
This is a Node JS API using Express for routes, Auth0 for security, and Axios to execute a third-party API call built within a Docker container. I've included the Docker commands I use, but you may need to brush up on others. [Add Link]
In general, the naming should be intuitive, where you would say, 'get this endpoint data' or 'post data to this endpoint'.
Feel free to use this as a starting point when building your own API.
-
In Visual Studio Code (or navigate to the directory), build the image using this command. It will build the server image from the directory and tag your image so that you can identify it if needed.
docker build . -t <user/node-api>
-
Then run this command to create a detached Docker container from the image and connect your localhost:5000 port to the 8080 port in the Docker container (which you'll notice is referenced as the default port in index.js).
docker run -p 5000:8080 -d test1/node-web-app
-
Go to localhost:5000. You should see 'Hello World'. If you do not, you may need to troubleshoot. More on that below in 'Troubleshooting'.
-
When you're ready to stop your docker container, you need to first determine the container's ID. Enter:
docker ps
-
You should see the name of your Image along with a Container ID. Use the Container ID in the next command to stop the container:
docker stop <Container ID>
Do NOT use these steps if you're also using Docker.
-
In Visual Studio Code (or navigate to the directory), run
nodemon index.js
. -
Go to localhost:8080 (notice that this is a DIFFERENT PORT). You should see 'Hello World'. If you do not, you may need to troubleshoot. More on that below in 'Troubleshooting'.
-
When you're ready to stop the API, use CTRL + C.
-
Use Postman to check each of the API URLs. The responses should be intuitive:
a. GET http://localhost:3000/
b. GET http://localhost:3000/api/courses
c. POST http://localhost:3000/api/courses
raw -> JSON input for the post
{ "name": "coursea 1" }
TIP: Rerun 2.b to confirm value was added
d. GET http://localhost:3000/api/courses/1
e. PUT http://localhost:3000/api/courses/1
raw -> JSON input for the post
{ "name": "coursea 1" }
TIP: Rerun 2.b to confirm value was updated
f. DELETE http://localhost:3000/api/courses/1
TIP: Rerun 2.b to confirm value was deleted
g. GET http://localhost:3000/api/hipster-ipsum
This is an example of a third-party API fetch and response.
-
To add Auth0, which enforces security credentials, you'll need to follow some tutorials first. If this is your first time, go ahead and do some reading since security is important and understanding what you're doing will save hours of frustration later.
a. Tutorials
i. https://auth0.com/docs/quickstart/backend/nodejs/01-authorization
ii. https://auth0.com/docs/quickstart/backend/nodejs/02-using
iii. https://auth0.com/docs/get-started/create-apps/machine-to-machine-apps
b. Validate set-up
GET http://localhost:3000/secure
header->Authorization (Bearer token as noted in the tutorial 3.a.ii above)
If the Docker container is not created in Step 2 of 'Using Docker', then you may need to use some other Docker commands to determine what's going on.
-
When you execute the
docker run
command, you will receive an output. I'm not 100% on the name, but let's call the output the 'run confirmation ID'. If you run this next command with that ID, you'll see an output of the script that's being run. If there is an error in your code, you'll find it there.docker logs <run confirmaton ID>
Having an issue with localhost not working.
- Double check that you have input the port correctly. You have to use port 5000 (or whatever you input in the Docker command), and you have to make sure you don't change any of the port 8080 references in the file. The 8080 port allows the app to move data in/out of the container at port 8080 while port 5000 is the port on your local machine that connects to Docker. So you have to use port 5000.
Having an issue with localhost not working.
- Double check that you have input the port correctly. You have to use port 8080 (unless you changed the port value in the API). The 8080 port allows the API to move data in/out of the app server on your local machine. So you have to use port 8080. This is a completely different experience than using Docker.
Feel free to reach out on Twitter, @elliotkoss. DMs open.
I intend to always keep this freely available, but if you'd like to send a donation, I'd be much appreciative.
ETH: elliotkoss.eth (ETH only set-up for this ENS - adding more wallets later)
Venmo: @Elliot-Koss
Cash App: $elliotkoss1
Use npm i --package-lock-only
to update the package-lock version before releasing.
Hello World! I built a Node JS API using Express for routes, Auth0 for security, and Axios to execute a third-party API call. I watched this great Node tutorial, read the Auth0 docs, and then used Postman's sample Node code plus some Googling to determine Axios was the right library for API calls.