Backend development with Express
  • Welcome!
  • Intro to backend development with Express
    • Tutorial
      • Getting started
      • Step 1: Setting up your project
      • Step 2: Installing Express
      • Step 3: Building the server
      • Step 4: Communicating with the server
      • Step 5: Routing
      • Step 6: Serving static files
      • Step 7: Sending your blog post to your server
      • Step 8: Extracting the blog post
      • Step 9: Saving your blog post
      • Step 10: Displaying your blog posts
      • Congratulations!
    • Extras
      • More ideas
      • Individual Blog Posts
    • Keywords
    • Command-line cheatsheet
Powered by GitBook
On this page
  • What is an endpoint?
  • 1. Create your own endpoints and send different responses
  1. Intro to backend development with Express
  2. Tutorial

Step 5: Routing

PreviousStep 4: Communicating with the serverNextStep 6: Serving static files

Last updated 6 years ago

At the moment our server only does one thing. When it receives a request from the / endpoint, it sends back the same response: "Yay Node Girls!".

Want to check? Try typing and see what happens.

However by making use of endpoints, we can make the server send different responses for different requests. This concept is called routing.

What is an endpoint?

An endpoint is the part of the URL which comes after /. For example: /chocolate is the "chocolate" endpoint. It's the URL to which you send a request.

1. Create your own endpoints and send different responses

We're going to try sending different responses at different endpoints. Remember the app.get() method? To set up routing in your server, we just need to repeat this method with different endpoints.

For example:

server.js
app.get("/", function (req, res) {
    res.send("Hello World!");
});

app.get("/chocolate", function (req, res) {
    res.send("Mm chocolate :O");
});

Challenge: Add some code so that your server sends one message when the endpoint is /node and another one when it's /girls.

http://localhost:3000/nodegirls