0
0
Expressframework~30 mins

Why REST principles matter in Express - See It in Action

Choose your learning style9 modes available
Why REST principles matter
📖 Scenario: You are building a simple web server using Express.js to handle requests for a small online bookstore. You want to organize your routes and responses following REST principles to make your API easy to understand and maintain.
🎯 Goal: Build a basic Express.js server with RESTful routes for books. You will create the initial data, set up a configuration variable, implement RESTful GET and POST routes, and complete the server setup.
📋 What You'll Learn
Create an array of book objects with exact titles and authors
Add a configuration variable for the server port
Implement RESTful GET and POST routes for books
Complete the Express server setup with middleware and listen on the port
💡 Why This Matters
🌍 Real World
RESTful APIs are used everywhere to connect web and mobile apps to servers. Understanding REST principles helps you build APIs that other developers can easily use and maintain.
💼 Career
Many software development jobs require building or working with REST APIs. Knowing how to structure routes and handle requests with Express.js is a valuable skill for backend development.
Progress0 / 4 steps
1
DATA SETUP: Create the initial books array
Create an array called books with these exact objects: { id: 1, title: 'The Hobbit', author: 'J.R.R. Tolkien' } and { id: 2, title: '1984', author: 'George Orwell' }.
Express
Need a hint?

Use const books = [ ... ] with two objects inside.

2
CONFIGURATION: Set the server port
Create a constant called PORT and set it to 3000.
Express
Need a hint?

Use const PORT = 3000; to set the port number.

3
CORE LOGIC: Implement RESTful GET and POST routes
Import Express, create an app with express(), add JSON middleware with app.use(express.json()), then create a GET route at /books that sends the books array, and a POST route at /books that adds a new book from req.body to books and sends the new book.
Express
Need a hint?

Use express() to create the app, add JSON middleware, then define GET and POST routes for /books.

4
COMPLETION: Start the server listening on the port
Add app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); }) to start the server.
Express
Need a hint?

Use app.listen(PORT, () => { console.log(...); }) to start the server.