const express = require('express');
const app = express();
const bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.get('/split/name', (req, res) => {
var name=req.query.fullName;
name=name.split(' ');
var first=name[0];
var second=name[1];
res.status(200).json({firstName: first,secondName:second});
});
// end split name
app.get('/calculate/age', (req, res) => {
var dob = req.query.dob;
var getAge = (dob) => {
var today = new Date();
var birthDate = new Date(dob);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
res.status(200).json({age: getAge(dob)});
});// i get the error here
The task requires creating an Express application with two routes that run on port 3000.
Route 1 - GET /split/name takes a fullName query parameter and outputs the firstName and lastName.
Sample input - /split/name?fullName=Aditya Kumar
Output - {
“firstName”:”Aditya”,
“lastName”:”Kumar”
}
Route 2 - /calculate/age takes the date of birth in yyyy-mm-dd format and calculates the person's age.
Sample input - /calculate/age?dob=1992-02-28
Output - {
“age”:27
}
Please note that using app.listen() is not required as it will be handled by the system.