javascript - 我正在学习创建 api,但只有 id 1 的响应才会发送其他人显示 404 错误
问题描述
我正在学习创建 api,但只有 id 1 的响应才会发送其他人显示 404 错误。我有用户数组,直到 id 8
const express = require('express')
const Joi = require('joi')
const app = express()
const users = require('./users')
app.get('/users', (req , res) => {
res.send(users)
})
app.get('/users/:id', (req , res) => {
for(let i=0; i<users.length; i++){
if(users[i].id !== parseInt(req.params.id)){
res.status(404).send('name with given id not found')
}else{
res.send(users[i])
}
}
})
app.listen(3000)
解决方案
推荐阅读
- forms - Google 表单 - 删除单个回复
- vba - 如何根据单元格中的值运行宏?
- ios - Swift3 iOS 中的 UITextField 中只允许使用数字和点字符
- c++ - C++ 最佳实践:接受 uint8_t、uint16_t、uint32_t、float 的函数
- angular - Angular 2+ 有一个主导入文件
- python-3.x - 在 Python 中获取 Zulu 时间戳的标准方法
- c# - MSTest 中的测试执行之间的延迟/暂停
- ios - 如何在 UIAlertController 中设置 UITextField 的高度?
- java - 打印机无法获取句柄
- npm - npm 运行任务 rollbackFailedOptional 永远