You are on page 1of 5

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();

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

app.listen(5000, () => {
console.log('Server has started!');
});

In the browser, send type and press http://localhost:5000/intro 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 blog.html. 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 http://localhost:5000/blog/1, then the output of the GET request should be:

Solution:

let express = require('express');

let app = express();

app.get('/blog/:id', function(req,res) {
console.log(req.params);
res.send(`
<div style="text-align: center">
<h1>Blog post ${req.params.id}</h1>
<h2>Blog post ${req.params.id} 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


`)
});

app.listen(5000, () => {
console.log('Server has started!');
});

In the browser, send type and press http://localhost:5000/blog/1 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();

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

app.get('/message', function(req,res) {
res.send(`
<h1>Message title</h1>
<p>Message body</p>
`)
});

app.get('/greet/:name', function(req,res) {
res.send(`

Full Stack Development Specialization Training Page 3 of 5


<h1>Hello ${req.params.name}!</h1>
<h2>Welcome to the page.</h2>
`)
});

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

app.listen(5000, () => {
console.log('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: http://localhost:5000/add?num1=10&num2=20


2. The output should be like this:

Solution:

let express = require('express');


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

app.get('/add', function(req,res) {
let inUrl = url.parse(req.url, true);
let nums = inUrl.query;

Full Stack Development Specialization Training Page 4 of 5


res.send(`
<h1>${nums.num1} + ${nums.num2} = ${Number(nums.num1) + Number(nums.num2)}</h1>
`)
});

app.listen(5000, () => {
console.log('Server has started!');
});

Full Stack Development Specialization Training Page 5 of 5

You might also like