Professional Documents
Culture Documents
Node Js Problem
Node Js Problem
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:
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.
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:
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>
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:
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(`
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!');
});
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:
Solution:
app.get('/add', function(req,res) {
let inUrl = url.parse(req.url, true);
let nums = inUrl.query;
app.listen(5000, () => {
console.log('Server has started!');
});