Unexpected syntax error encountered when shutting down the route, unable to recover

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.

Answer №1

**Everything appears to be functioning properly, but you just need to incorporate app.listen to make the server listen on a specific port **

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 encounter an issue here

app.listen(3000, ()=>{
    console.log("Server is listening on port 3000")
})

Feel free to try out any of the following URLs

Successfully tested locally https://i.sstatic.net/rFI8h.png

https://i.sstatic.net/cvKX4.png

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error 404 on Nuxt.js routes when using IISNODE in Internet Information Services

Currently attempting to host a demo of nuxt.js and express.js on IIS with iisnode. Unfortunately, I am encountering 404 errors for the nuxt page routes, while the express API routes are functioning correctly. The setup involves allowing express to manage ...

The addClass() method seems to be malfunctioning following an ajax request

My current project involves setting up an AJAX call that is triggered when a user clicks on an anchor link. Once the AJAX operation is successful, I want to dynamically add a class to the specific anchor that initiated the call. The script itself seems to ...

Incorporating video.js into an Angular website application

I've encountered a strange issue while attempting to integrate video.js into my angular app. <video id="example_video_1" class="video-js vjs-default-skin" controls preload="none" width="300" height="264" poster="http://video-js.zenco ...

Unable to bring in Vue component from NPM module

Hello there, I recently developed my own npm package for a navigation bar and I need to incorporate it into my main codebase. Currently, I am utilizing vue @components but I am struggling with integrating the imported component. If anyone has insight on h ...

Live tweet moderation made easy with the Twitter widget

Currently in search of a widget, jQuery plugin, hosted service, or equivalent that can be easily integrated into an HTML page. This tool should display and automatically update live tweets featuring a specific hashtag, only from designated accounts set by ...

Scrolling animations tailored to the navbar using jQuery

My fixed header contains a few links, and I've written some code that almost works perfectly. The issue is that there's a delay when the second function is executed. While it successfully scrolls to the top of the page when the linked element is ...

Communication Error between Amazon Elastic Beanstalk of express server and Netlify React App

I am encountering an error while attempting to deploy my React application that communicates with a backend hosted on Amazon Elastic Beanstalk. The error I receive is: "Access to XMLHttpRequest at 'https://4bto.sitename.com/api/dashboard/login' f ...

Issue with dropdown not toggling open when using focus and focusout events

I am facing an issue with two dropdowns having the same class, referred to as "dropdown" in this scenario. I created a fiddle using jQuery to manipulate these dropdowns: $('.dropdown').focus(function () { //Code to manipulate this dropdown }) ...

Tips for handling arguments in functional components within React applications

Is it possible to pass arguments to a function in a functional component without creating the function directly in JSX? I've heard that creating functions in JSX is not recommended, so what's a better way to achieve this? function MyComponent(pr ...

Guide to utilizing AJAX to retrieve a value from a controller and display it in a popup

I need assistance with retrieving data from a controller and displaying it in an HTML pop-up when a button is clicked. Currently, the pop-up shows up when the button is clicked, but the data is not being loaded. I would like the values to be loaded and d ...

Strategies for setting up the runtime dynamically for Nextjs API routes

After deploying my Next.js 13 app on Cloudflare Pages, I encountered an issue with the API routes. To address this, I had to export the runtime variable from each route in the following manner. export const runtime = "edge" During local developm ...

Tally the number of words entered in the text box

Is there a way to make the keyword search in a text area live, rather than requiring the user to manually initiate the search? I have a working code that counts the number of times a specific keyword is used within the text, but it currently requires the u ...

Guide on altering the cell's background hue depending on its value through javascript

I'm dealing with a table that has 3 columns: field1 is a Category field2 and field3 contain Measures (specifically integers 1, 2, 3, 4, and 5). Is there a way to use Javascript to conditionally format the background color of cells in the table hol ...

Calculating the Bounding Box of SVG Group Elements

Today I encountered a puzzling scenario involving bounding box calculations, and it seems that I have yet to fully understand the situation. To clarify, a bounding box is described as the smallest box in which an untransformed element can fit within. I h ...

Express Router Setup Error: The Router.use() method is expecting a middleware function, but instead received an undefined value

First and foremost, I want to clarify that I am well aware of the plethora of questions posed on this platform with a similar title. I have already attempted the suggestions provided there, but unfortunately, they either did not work for me or were too sp ...

No matter how hard I try, the async function within the React Component keeps returning 'Promise {<pending>}' consistently

Currently, I'm facing an issue where running an asynchronous function inside a functional component in React (NextJS) results in the function returning a pending promise: Promise {<pending>}. Oddly enough, fetching data from a dummy API works pe ...

Save a JSON object from a URL into a JavaScript variable using jQuery

I am trying to figure out how to use the .getJSON() function to store a JSON object as a JavaScript variable. Can someone guide me through this process? var js = $.getJSON(url, function(data) {...} ); Is it necessary to include the function(data) { ...

Setting up the data for the AjaxAppender Request Log4Javascript

I am completely new to Log4Javascript and the concept of logging, so please bear with me. My current task involves sending logs to a CouchDB server using a POST request. However, I keep encountering an error from the server: {"error":"bad_request","reaso ...

Logging in with passport in a node.js app

I'm facing challenges setting up passport.js for authentication in my node.js, express, mongoose application. Despite trying different examples on the internet, I keep getting stuck because I don't fully grasp the flow of passport.js processing. ...

Issue encountered when using express-session in conjunction with the express framework

Challenge: I encountered an issue while trying to utilize session = require("express-session"). Despite previously installing express-session using npm install express-session, I am prompted with an error message: vishal@rocker:~/clg/project1/node_module ...