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?