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 syntaxconst express = require("express");
const app = express();
app.get("/", (request, response) => {
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 syntaxconst express = require("express");
const app = express();
app.get("/gadgets", (request, response) => {
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,
Method | Path | Description |
---|---|---|
GET | / | Will send the text Home Page as response |
GET | /about | Will send the text About Page as response |
Export the express instance using default export syntax.
Use Common JS module syntaxconst express = require("express");
const app = express();
app.get("/", (request, response) => {
response.send("Home Page");
});
app.get("/about", (request, response) => {
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 syntaxconst express = require("express");
const app = express();
app.get("/", (request, response) => {
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 syntaxnpm 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("/", (request, response) => {
const myDate = dateTimeFNS(new Date(), 100);
response.send(
`${myDate.getDate()}/${myDate.getMonth() + 1}/${myDate.getFullYear()}`
);
});
app.listen(3000);
module.exports = app;