【问题标题】:Unrecoverable syntax error while closing route关闭路由时出现不可恢复的语法错误
【发布时间】:2020-07-15 13:58:48
【问题描述】:
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

根据问题,我得到的输入是“/split/name?fullName=ritik verma”,我必须将其拆分为名字和姓氏 第二部分是“/calculate/age?dob=17-04-1999”,我需要计算年龄。

所以我给你一个实际的问题可能会有所帮助

问题:-

创建一个具有以下路由并在端口 3000 上运行的 Express 应用程序 -

Route 1 - GET /split/name - 将 fullName 作为查询参数并给出 firstName 和 姓氏作为输出。

示例输入 - /split/name?fullName=Aditya Kumar

输出 - {

“名字”:“阿迪亚”,

“lastName”:”Kumar”

}

Route 2 - /calculate/age - 以 yyyy-mm-dd 格式获取出生日期和 返回此人的年龄。

示例输入 - /calculate/age?dob=1992-02-28

输出 - {

“年龄”:27

}

注意:您不需要使用 app.listen()。这将由系统处理。

【问题讨论】:

  • 你遇到了什么错误?
  • 会不会是日期字符串的格式?
  • 17.04.1999 是一种非标准日期格式,因此您的 getAge 会生成 NaN。我猜这无法正确序列化,因此您会收到错误。
  • 它要求添加';'在“返回年龄”上方的右大括号之后

标签: javascript express


【解决方案1】:

**代码似乎工作正常,只需要添加 app.listen 服务器来监听特定端口 **

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

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

现在尝试以下任何一种方法

在本地测试

【讨论】:

  • 你我也找不到任何错误。也许它与在线编译器有关。
  • 我试过它在本地工作得很好。我正在使用节点版本 10.15.0。请验证您的。在本地测试(在上面添加了屏幕截图)它工作正常。
  • 你也可以上传年龄一号
  • 当然,也请找出一岁。希望这对您有所帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-01-10
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 1970-01-01
  • 2015-03-09
  • 2023-03-09
相关资源
最近更新 更多