create express route in node js to calculate age

115
January 10, 2020, at 8:20 PM

Create an Express application that has following routes and runs on port 3000 - Route 1 - GET /split/name - which takes fullName as query parameter and gives firstName and lastName as output. Sample input - /split/name?fullName=Aditya Kumar Output - { “firstName”:”Aditya”, “lastName”:”Kumar” } Route 2 - /calculate/age - which takes date of birth in format yyyy-mm-dd and return the age of the person. Sample input - /calculate/age?dob=1992-02-28 Output - { “age”:27 }

NOTE: You are not required to use app.listen(). This will be handled by the system.

for above question my solution is

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) => {
let fullname=req.query.fullName;
let name=fullname.split(" ");
res.send(`firstName:${name[0]},lastName:${name[1]}`);

});// end split name


app.get('/calculate/age', (req, res) => {
let dob=req.query.dob;
function getAge(dob) {
    var today = new Date();
    var nowyear = today.getFullYear();
    var nowmonth = today.getMonth();
    var nowday = today.getDate();
    var birthyear = dob.getFullYear();
    var birthmonth = dob.getMonth();
    var birthday = dob.getDate();
    var age = nowyear - birthyear;
    var age_month = nowmonth - birthmonth;
    var age_day = nowday - birthday;
    if(age_month < 0 || (age_month === 0 && age_day <0)) {
            age = parseInt(age) -1;
        }
    return age;
}
res.send(`age:${getAge()}`);
});
module.exports = app;

what is wrong with my error or you can give your solution also

READ ALSO
How can I access the content from a file in another file?

How can I access the content from a file in another file?

I am currently trying to build a specific app: I want to transform xml-files in a directory and have a <div> created for each of them

90
After session destroyed and goes -1 in historie session is still &ldquo;active&rdquo; | NodeJS &amp; Express

After session destroyed and goes -1 in historie session is still “active” | NodeJS & Express

I have a login system and when the I log out (session is destroyed)Then when I go back to the page where I logged out from it still loads, but first when reload the page then it sees that the session is destroyed

89
Node Electron synch text append

Node Electron synch text append

I am trying to make an Electron app that will read given directory recursively and print file names to userLuckily I found a function on GitHub which makes exactly what I want

75
Data and salt arguments required error (authorization)

Data and salt arguments required error (authorization)

I'm a beginner in NodeJS and I've tried to make an authentication form using NodeJS + expressI want to make a validation for my password (when "confirmpassword" is different than "password" it should return nothing

198