Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Your First Route | Section
Building APIs with Express.js

bookYour First Route

Swipe to show menu

A server becomes useful when it can respond to requests. In Express, this is done using routes.

A route defines what should happen when a user accesses a specific URL.

const express = require('express');

const app = express();

app.get('/', (req, res) => {
  res.send('Hello from server');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Here, app.get() defines a route for the '/' path. This path represents the homepage.

When a user opens http://localhost:3000, the server runs the function and sends a response using res.send().

The req object represents the incoming request, and the res object is used to send a response back.

Now your server not only runs but also responds to requests.

question mark

What does res.send() do?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 3

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 3
some-alt