Express.JS Coding Practice 3

 Get a String

Given an

app.js
file, write an API with path
/
using express JS to send
Express JS
text as a response.

Export the express instance using default export syntax.

Use Common JS module syntax

const express = require("express");
const app = express();

app.get("/", (requestresponse=> {
  response.send("Express JS");
});
app.listen(3000);

module.exports = app;

Gadgets Page

Given two files

app.js
and
gadgets.html
, write an API in
app.js
file for the path
/gadgets
that sends the
gadgets.html
file as a response.

Export the express instance using default export syntax.

Use Common JS module syntax

const express = require("express");
const app = express();

app.get("/gadgets", (requestresponse=> {
  response.sendFile("gadgets.html", { root__dirname });
});
app.listen(3000);

module.exports = app;

API Routing

API Routing

Given an

app.js
file, write two APIs that sends different strings as responses.

Refer to the below table for paths and responses,

MethodPathDescription
GET/Will send the text 
Home Page
 as response
GET/aboutWill send the text 
About Page
 as response

Export the express instance using default export syntax.

Use Common JS module syntax
const express = require("express");
const app = express();

app.get("/", (requestresponse=> {
  response.send("Home Page");
});

app.get("/about", (requestresponse=> {
  response.send("About Page");
});
app.listen(3000);
module.exports = app;

Today's Date

Today's Date

Given an

app.js
file, write an API with path
/
using express JS that sends today's date as a response in
DD-MM-YYYY
format.

Export the express instance using default export syntax.

Use Common JS module syntax

const express = require("express");
const app = express();

app.get("/", (requestresponse=> {
  const todayDate = new Date();
  response.send(
    `${todayDate.getDate()}-${
      todayDate.getMonth() + 1
    }-${todayDate.getFullYear()}`
  );
});

app.listen(3000);

module.exports = app;
Date after 100 Days from Today

Date after 100 Days from Today

Given an

app.js
file, write an API with path
/
using express JS to send the date after 100 days from today as a response in
DD/MM/YYYY
format.

Export the express instance using default export syntax.

Use the third-party package

date-fns
.

Use Common JS module syntax
npm install date-fns --save
npm install express --save

const express = require("express");
const app = express();

const dateTimeFNS = require("./node_modules/date-fns/addDays");

app.get("/", (requestresponse=> {
  const myDate = dateTimeFNS(new Date(), 100);
  response.send(
    `${myDate.getDate()}/${myDate.getMonth() + 1}/${myDate.getFullYear()}`
  );
});

app.listen(3000);

module.exports = app;


Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form