node.js - 尝试测试服务器连接时 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> at JSON.parse (<anonymous>)<br> at parse (C:\Users\djaad\Desktop\smart-brain-api\node_modules\body-parser\lib\types\json.js:89:19)<br> at C:\Users\djaad\Desktop\smart-brain-api\node_modules\body-parser\lib\read.js:121:18<br> at invokeCallback (C:\Users\djaad\Desktop\smart-brain-api\node_modules\raw-body\index.js:224:16)<br> at done (C:\Users\djaad\Desktop\smart-brain-api\node_modules\raw-body\index.js:213:7)<br> at IncomingMessage.onEnd (C:\Users\djaad\Desktop\smart-brain-api\node_modules\raw-body\index.js:273:7)<br> at IncomingMessage.emit (events.js:317:22)<br> at endReadableNT (_stream_readable.js:1215:12)<br> at processTicksAndRejections (internal/process/task_queues.js:84:21)</pre>
</body>
</html>
我不知道是什么原因造成的,请你帮忙吗?
谢谢,
大卫。
解决方案
推荐阅读
- android - 我应该在 Singleton 类中处理一次性用品吗(Android RxJava 2)
- git - 如何删除 git 中不完整的拉取文件?
- semantic-web - 公证领域的本体
- ios - Swift 4. 能否建议您在返回应用后如何播放视频?
- javascript - 如何在Javascript中获取给定日期,时间和区域的时间戳
- javascript - 激活引导折叠时切换 Inline Chevron Down
- hyperledger-composer - 错误:尝试 ping 时出错。错误:2 UNKNOWN:确保链码
- c# - 有什么区别,为什么 Switch Case 在 C# 中这样工作?
- docker - 我使用 nvdidia-docker 使用 nvidia-docker 中的 nvidia/cuda 映像安装 mysql5.7。发现mysql服务无法启动
- javascript - Webpack 无冗余导出所有组件