I'm running a simple backend server to select a single product from my products.json database. However, it keeps sending me this message: Cannot GET /api/products/1
Here is my server.js code:
const express = require('express');
const products = require('./data/products')
const app = express();
app.get('/', (req, res) => {
res.send('API is running...');
});
app.get('/api/products', (req, res) => {
res.json(products);
});
app.get('/api/products:id', (req, res) => {
const product = products.find((p) => p._id === req.params.id);
res.json(product);
});
app.listen(5000, console.log('Server running on port 5000'));
CodePudding user response:
Try
app.get('/api/products/:id', (req, res) => {
const product = products.find((p) => p._id === req.params.id);
res.json(product);
});
You were missing a "/" between products and :id