'I cannot get my JavaScript REST API to return data

I know there are many similar questions about this issue and most of them are already answered, but I am a beginner on this.

This code is a code from a YouTube tutorial about the basic of making REST API, as follows:

const app = require('express')();
const PORT = 8095;

app.get('/tshirt', (req, res) => {
    res.status(200).send({
        tshirt: "red white tshirt",
        size: "large"
    })
});

I tried to access get tshirt both from browser and Insomnia and it says 'could not get /tshirt', what might be wrong?



Solution 1:[1]

You also need to start the actual server.

const express = require('express');
const app = express();
const port = 8095;

app.get('/tshirt', (req, res) => {
   res.status(200).send({
        tshirt: "red white tshirt",
        size: "large"
    })
});

app.listen(port, () => {
  console.log(`Example app listening on port ${port}`)
})

As per https://expressjs.com/en/starter/hello-world.html

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Peter Pajchl