简体   繁体   中英

Node.js server running but not loading on browser

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在此处输入图像描述

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!

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

There is a problem with usage of body-parser.

You have app.use(bodyParser.json)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM