首页 > 解决方案 > 我正在学习创建 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)

标签: javascriptnode.js

解决方案


推荐阅读