gpt4 book ai didi

javascript - Express app.get 中间件不工作 `SyntaxError: Unexpected end of input`

转载 作者:行者123 更新时间:2023-12-02 21:33:50 24 4
gpt4 key购买 nike

我正在关注 Youtube 上的 Express 教程。不幸的是,我遇到了中间件的问题,并且非常困惑。

在我的主文件中,我有代码:

const express = require('express');
const path = require('path');
const app = express();
const logger = require('./middleware/logger');

app.use('/api/members', require('./routes/api/members'));

const PORT = process.env.PORT || 5000;

app.listen(PORT, () => console.log(`Server started on port ${PORT}`));

members.js 文件是:

const express = require('express');
const router = express.Router();
const members = require('../../Members')

//Gets All Members
router.get('/', (req, res) => res.json(members));

//Get Single Memberss
router.get('/:id', (req, res) => {
const found = members.some(member => member.id === parseInt(req.params.id));

if(found){
res.json(members.filter(member => member.id === parseInt(req.params.id)));
} else {
res.status(400).json({msg: `No member with the id of ${req.params.id}`});
}

module.exports = router;

此代码导致 nodemon 崩溃并给出以下错误:

SyntaxError: Unexpected end of input
at wrapSafe (internal/modules/cjs/loader.js:1072:16)
at Module._compile (internal/modules/cjs/loader.js:1122:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178
:10)
at Module.load (internal/modules/cjs/loader.js:1002:32)
at Function.Module._load (internal/modules/cjs/loader.js:901:14)
at Module.require (internal/modules/cjs/loader.js:1044:19)
at require (internal/modules/cjs/helpers.js:77:18)
at Object.<anonymous> (/home/gavin/Documents/ExpressCrashCourse/index
.js:12:25)
at Module._compile (internal/modules/cjs/loader.js:1158:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1178
:10)

我的解释是它在 app.use('/api/members', require('./routes/api/members')); 处中断,但我不太明白为什么。另外,在上面的 app.use 函数中,我不太明白第二个参数在做什么。我认为这将是 Express 中 Router 对象的一个​​实例,但我不明白它如何替代通常的 function(req,res){}那将是它的位置。不管怎样,我已经看过好几次了,看起来我从教程中复制了完全按照其编写的代码,所以我真的很困惑为什么这不起作用。非常感谢任何帮助。

最佳答案

看起来您缺少右花括号 } 和括号 )

const express = require('express');
const router = express.Router();
const members = require('../../Members')

//Gets All Members
router.get('/', (req, res) => res.json(members));

//Get Single Memberss
router.get('/:id', (req, res) => {
const found = members.some(member => member.id === parseInt(req.params.id));

if (found) {
res.json(members.filter(member => member.id === parseInt(req.params.id)));
} else {
res.status(400).json({msg: `No member with the id of ${req.params.id}`});
}
}); // <- Close your callback
module.exports = router;

关于javascript - Express app.get 中间件不工作 `SyntaxError: Unexpected end of input`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60556013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com