Node.js server running but not loading on browser

Viewed 36

I know this is a simple problem but I have been looking for a solution for the last 2 days and can't seem to find it. I am following a tutorial where we've set up an express server. I think the two relevant files are app.js and auth.js.

APP.js

//PACKAGES
    //"mongodb://0.0.0.0:27017/test"
require('dotenv').config()
    
const mongoose = require('mongoose')
const express = require("express")
const app = express()
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser')
const cors = require('cors')

//LOCAL PACKAGES
const authRoutes = require("./routes/auth")



//CONNECTING TO MONGODB
mongoose.connect(process.env.DATABASE, {
useNewUrlParser: true,    
useUnifiedTopology: true,
useCreateIndex:true
}).then(() => {
    console.log(`DB CONNECTED!!!!!`)
    
 })

 //MIDDLEWARE
 app.use(bodyParser.json)
 app.use(cookieParser())
 app.use(cors())
 //ROUTES
app.use("/api", authRoutes)


//PORT
const port = process.env.PORT || 8000;
//STARTING A SERVER
app.listen(port, ()=> {
    console.log(`app is running at ${port}`)
})

AUTH.js

const express = require('express')
const router =  express.Router()


router.get("/signout", (req,res)=> {
    res.send("user signout")
})

module.exports = router;

In the console, I see that my server is connected to DB and running on port no 8000 enter image description here

However, when I go to the browser and write http://localhost:8000/api/signout, a spinning wheel that never stops and it does not return my request. I have tried to match the author's code, turn off the firewall, and changed the port number but nothing works. I would be grateful if someone helps as I am stuck on this problem and I want to progress. Thank you!

P.S: my github repo: https://github.com/timothyroybd/ecommerce-website

1 Answers

There is a problem with usage of body-parser.

You have app.use(bodyParser.json)

and should be app.use(bodyParser.json())

Related