donut-shop
Build a Server from Scratch
Setup
- Create a
server.js
npm init -y
to generate apackage.json
npm install express
Build the Basic Server
- Require express
- Create an instance of express.
- Set the PORT (be sure to include process.env.PORT for deploying to Heroku)
- Listen on the PORT
- Add middleware
Build Routes
- Optional: Build a test route:
"/api/config"
View Routes
- Always going to be get routes.
- Typically send back HTML for the browser to parse and display.
- Use
res.sendFile
to send back a previously built HTML file.
To Send Back an HTML File
- Build a get route.
- Call
res.sendFile
- Use
path.join()
to combine the __dirname with the file name. (npm install path
)
API Routes
- Resource-driven API Development
- GET "/api/resources" returns a collection of resources.
- GET "/api/resources/:id" return a single resource.
- POST "/api/resources" allows the user to create a new resource.