简体   繁体   中英

I can't get my data back, The Post function is working fine, tested with postman

I can't get my data, Post function is working fine I'm using Postman for testing it. When I put localhost:3000/api/names/2 , I get this error

Can not GET /api/names/2

const express = require('express');
const app = express(); 
var cors =require('cors')
app.use(express.json()); app.use(cors())
const names = []

app.get('/api/names/', (req, res) => {
res.send(names);

} );

app.post('/api/names', (req, res) => {
     const name = {
         id: names.length,
         name :req.body.name

     };
     names.push(name);
     res.send(name); 
 });

app.listen(3000, () => console.log('port 3000'));

要得到

app.get('/api/names/:id', (req, res) => { res.send(names.filter(x => x.id == req.params.id););

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM