Back End Development and APIs Projects - Timestamp Microservice: A simple POST request works in postman but not in browser

This is my code:

// index.js
var express = require('express')
var app = express()
const router = require("./Router/route")

app.use(express.json())
app.use(router)

var listener = app.listen(process.env.PORT || 5000, function () { 
  console.log('Your app is listening on port ' + listener.address().port);
})
// Router/route.js
const express = require("express")
const router = express.Router()
const currentTime = require("../Controllers/currentTime")

router.route("/api").post(currentTime)

module.exports = router
// Controllers/currentTime.js
const { StatusCodes } = require("http-status-codes")

const currentTime = (req, res) => {
    const dateObject = new Date()

    if (dateObject == "Invalid Date") {
        return res.status(StatusCodes.BAD_REQUEST).json({ error: "Invalid Date" })
    }
    
    const unix = dateObject.getTime() / 1000

    let day = dateObject.getDay()
    const daysArray = ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"]
    day = daysArray[day]

    let month = dateObject.getMonth()
    const monthsArray = ["Jan", "Feb", "Mar", "Apr", "May","June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"]
    month = monthsArray[month]

    const year = dateObject.getFullYear()
    const date = dateObject.getDate()

    res.status(StatusCodes.OK).json({ 
        unix: unix,
        utc: `${day}, ${date} ${month} ${year} 00:00:00 GMT`
    })
}

module.exports = currentTime

The postman sends the correct response:

But when I enter this URL: localhost:5000/api , the browser doesn’t return any response
It throws an error : Cannot GET /api

(Note: I can’t upload the screenshot of the browser because new users on FCC forum can only upload one media per question)

Why is this happening?
If Postman sends the correct response, the browser should return the correct response too, right?

Navigating to a URL is a GET, not a POST and you do not have a GET handler (Cannot GET /api).

Firefox does have an “Edit and Resend” option that lets you do a POST but I wouldn’t use that. You can use a form to test the POST or just keep using a tool like Postman.


BTW. The unix value should not be a decimal number.

@lasjorg
So, basically:

(a) To test a GET request on browser, I just need to insert the URL in the browser

(b) but to test a POST request on a browser, I would need to use a form

right?

You can also write client-side JS using fetch that does a POST.

It is easier to use something like Postman to test your API.