首页 > 解决方案 > 尝试测试服务器连接时 Express Server 意外令牌

问题描述

我正在使用 Clarifi API 设置一个小型面部识别应用程序,我正在使用快速服务器并在使用 Postman 时进行测试。我有四个端点:

这是我的文件 server.js 中的代码

const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());

const database = {
    users: [
        {
            id: '123',
            name: 'David',
            email: 'david@david.com',
            password: 'cookies',
            entries: 0,
            joined: new Date()

        },
        {
            id: '124',
            name: 'John',
            email: 'john@david.com',
            password: 'apples',
            entries: 0,
            joined: new Date()

        }
    ]
}
app.get('/', (req, res) => {
    res.send(database.users)
})

app.post('/signin', (req, res) => {
    if (req.body.email === database.users[0].email && 
        req.body.password === database.users[0].password){
            res.json('success')
        } else {
            res.status(400).json('error logging in');
        }
})

app.post('/register', (req, res) => {
    const { email, name, password } = req.body;
    database.users.push({
        id: '125',
            name: name,
            email: email,
            password: password,
            entries: 0,
            joined: new Date()
    })
    res.json(database.users[database.users.length-1])

})

app.get('/profile/:id', (req, res) => {
    const { id } = req.params;
    let found = false;
    database.users.forEach(user => {
        if (user.id === id) {
            found = true;
            return res.json(user);
        }
    })
    if (!found) {
        res.status(404).json('User not found');
    }
})

app.post('/image', (req, res) => {
    const {id} = req.body;
    let found = false;
    database.users.forEach(user => {
        if(user.id === id) {
            found = true;
            user.entries++
          return res.json(user.entries);
        }
    }) 
    if(!found){
        res.status(400).json('NOT FOUND'); 
    }
})  

app.listen(3000, ()=> {
    console.log('app is running on port 3000');
})

一切正常,直到我添加这部分,

app.post('/image', (req, res) => {
    const {id} = req.body;
    let found = false;
    database.users.forEach(user => {
        if(user.id === id) {
            found = true;
            user.entries++
          return res.json(user.entries);
        }
    }) 
    if(!found){
        res.status(400).json('NOT FOUND'); 
    }
}) 

我知道它的功能与配置文件几乎相同,我最终会将它取出并创建它以供任何地方使用,但这里的主要内容是让它在每次上传图像时创建条目的最终端点上工作对于每个用户

就像我说的,在添加这部分代码之前,一切都在 Postman 中运行,另外,我使用的是 VS 代码,代码中没有错误,一切都在那里检查,我还在终端中运行 npm start 并在我尝试邮递员之前,获得应用程序在端口 3000 上运行的正确响应。

以下是 Postman 的正文输出/错误的副本

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>Error</title>
</head>

<body>
    <pre>SyntaxError: Unexpected token } in JSON at position 16<br> &nbsp; &nbsp;at JSON.parse (&lt;anonymous&gt;)<br> &nbsp; &nbsp;at parse (C:\Users\djaad\Desktop\smart-brain-api\node_modules\body-parser\lib\types\json.js:89:19)<br> &nbsp; &nbsp;at C:\Users\djaad\Desktop\smart-brain-api\node_modules\body-parser\lib\read.js:121:18<br> &nbsp; &nbsp;at invokeCallback (C:\Users\djaad\Desktop\smart-brain-api\node_modules\raw-body\index.js:224:16)<br> &nbsp; &nbsp;at done (C:\Users\djaad\Desktop\smart-brain-api\node_modules\raw-body\index.js:213:7)<br> &nbsp; &nbsp;at IncomingMessage.onEnd (C:\Users\djaad\Desktop\smart-brain-api\node_modules\raw-body\index.js:273:7)<br> &nbsp; &nbsp;at IncomingMessage.emit (events.js:317:22)<br> &nbsp; &nbsp;at endReadableNT (_stream_readable.js:1215:12)<br> &nbsp; &nbsp;at processTicksAndRejections (internal/process/task_queues.js:84:21)</pre>
</body>

</html>

我不知道是什么原因造成的,请你帮忙吗?

谢谢,

大卫。

标签: node.jsexpresspostman

解决方案


推荐阅读