0% found this document useful (0 votes)
4 views5 pages

Express App GET Request Examples

The document discusses coding problems involving creating GET requests and routes in Express.js. It provides code solutions to create simple routes like '/intro' and '/greet' that return HTML, routes with parameters like '/blog/:id' that return dynamic content, and routes that process URL query parameters like '/add' to add two numbers.

Uploaded by

jishika491
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Express App GET Request Examples

The document discusses coding problems involving creating GET requests and routes in Express.js. It provides code solutions to create simple routes like '/intro' and '/greet' that return HTML, routes with parameters like '/blog/:id' that return dynamic content, and routes that process URL query parameters like '/add' to add two numbers.

Uploaded by

jishika491
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Coding problem 28

Create an express app. Then, create new GET request with the endpoint /intro that sends back a
very simple introduction. You can make the message anything you want. It should look something
similar to the below screenshot:

Solution:

let express = require('express');


let app = express();

[Link]('/intro', function(req,res) {
[Link](`
<h1>Hi there!</h1>
<h2>I'm Aarthi. How are you?</h2>
`)
});

[Link](5000, () => {
[Link]('Server has started!');
});

In the browser, send type and press [Link] in the address bar to see
the output.

Full Stack Development Specialization Training Page 1 of 5


Coding problem 29

Use express’s built-in methods to retrieve the details of a HTML file [Link]. But the catch is, the
request URL must contain a Unique id tagged to it, and the output should reflect that id in the title.

1. For example, if the url is [Link] then the output of the GET request should be:

Solution:

let express = require('express');

let app = express();

[Link]('/blog/:id', function(req,res) {
[Link]([Link]);
[Link](`
<div style="text-align: center">
<h1>Blog post ${[Link]}</h1>
<h2>Blog post ${[Link]} subtitle</h2>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tincidunt, sapien
vitae vestibulum tristique, mauris dui efficitur tortor, tempus viverra massa lacus nec
sem. Proin risus felis, porta vel lorem vitae, lobortis pulvinar sapien. Integer
condimentum libero in nibh ullamcorper pharetra. Integer faucibus rutrum eros, ac
tincidunt felis consectetur sollicitudin. Nunc sed ultrices ex, in gravida diam. </p>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed tincidunt, sapien
vitae vestibulum tristique, mauris dui efficitur tortor, tempus viverra massa lacus nec
sem. Proin risus felis, porta vel lorem vitae, lobortis pulvinar sapien. Integer
condimentum libero in nibh ullamcorper pharetra. Integer faucibus rutrum eros, ac
tincidunt felis consectetur sollicitudin. Nunc sed ultrices ex, in gravida diam. </p>
</div>

Full Stack Development Specialization Training Page 2 of 5


`)
});

[Link](5000, () => {
[Link]('Server has started!');
});

In the browser, send type and press [Link] in the address bar to see
the output.

Coding problem 30
Create multiple GET requests in express for different endpoints, like the following:
1. /greet -> just displays a simple ‘Hello there, Welcome message’. You can design it however
you want in HTML.
2. /message -> Just print a simple message.
3. /intro -> An introduction about yourself or a made-up person.
4. /greet/:name -> Personalized greeting based on the name you get in the parameters.

Solution:

let express = require('express');


let app = express();

[Link]('/greet', function(req,res) {
[Link](`
<h1>Hello there!</h1>
<h2>Welcome to the page.</h2>
`)
});

[Link]('/message', function(req,res) {
[Link](`
<h1>Message title</h1>
<p>Message body</p>
`)
});

[Link]('/greet/:name', function(req,res) {
[Link](`

Full Stack Development Specialization Training Page 3 of 5


<h1>Hello ${[Link]}!</h1>
<h2>Welcome to the page.</h2>
`)
});

[Link]('/intro', function(req,res) {
[Link](`
<h1>Hi there!</h1>
<h2>I'm Aarthi. I'm a programmer. </h2>
`)
});

[Link](5000, () => {
[Link]('Server has started!');
});

Try the different endpoints to check them now.

Coding problem 31
Create a /add endpoint. Get the numbers 1 and 2 from the request URL, parse them, add them, and
display the result on the browser, like this:

1. The URL should be like this: [Link]


2. The output should be like this:

Solution:

let express = require('express');


let url = require('url');
let app = express();

[Link]('/add', function(req,res) {
let inUrl = [Link]([Link], true);
let nums = [Link];

Full Stack Development Specialization Training Page 4 of 5


[Link](`
<h1>${nums.num1} + ${nums.num2} = ${Number(nums.num1) + Number(nums.num2)}</h1>
`)
});

[Link](5000, () => {
[Link]('Server has started!');
});

Full Stack Development Specialization Training Page 5 of 5

You might also like