Lesson 2 Learn Express basics
Servers / HTTP API / CRUD
What are HTTP API’s?
What are HTTP API’s?
At a high level the API is just an interface for talking to our server using the http protocol
What we know about API calls from course 1
API endpoints and CRUD operations
Let’s build our first Express Server!
"Building an Express server is like riding a bike - except the bike is on fire, and the road is also on fire, and everything is on fire because you're using JavaScript" - probably Steve Jobs
Setting up our local environment
To set up a new Node.js project, follow these steps:
// Import the Express library
const express = require("express");
// Create a new instance of the Express server
const server = express()
// Use the built-in JSON middleware to parse incoming JSON requests
server.use(express.json())
// Set up a route to handle GET requests to the root path
server.get("/", (req, res) => {
// Send a JSON response with a "message" property set to "I'm alive!"
res.status(200).json({ message: "I'm alive!" });
});
// Set the server to listen on the provided port, or 5500 if no port is specified
const PORT = process.env.PORT || 5500;
server.listen(PORT, () => {
// Log a message to the console when the server starts listening
console.log(`Server listening on port ${PORT}`);
});
Calling our new API endpoint
Success!
The elements of an Express endpoint
Review
Resources