TransWikia.com

Localhost/5000 not working. How do i get this ro work when i run node server.js

Stack Overflow Asked by Nyasha Majome on January 28, 2021

i have a problem, which when im trying to run my localhost/5000 its not working. I cant also get and post data on postman because of that. Anyone has an idea of how i can fix this ? However when i run the code it is showing that it is connected to my DB. I have also create an env file with the DB link and set the PORT to 5000

Here is the code:
server.js

const express = require('express')
const mongoose = require('mongoose')
const cors = require('cors')
const fileUpload = require('express-fileupload')
const cookieParser = require('cookie-parser')
const PORT = process.env.PORT || 5000

const app = express();
app.use(express.json())
app.use(cookieParser())
app.use(cors)
app.use(fileUpload({
    useTempFiles: true
}))

//connect to db

const URI = process.env.MONGO_URL
mongoose.connect(URI,{
    useCreateIndex: true, 
    useFindAndModify: false,
    useNewUrlParser:true,
    useUnifiedTopology:true
}, err=> {
        if (err) throw err;
        console.log('Connected to Mongo DB')
})

// app.get('/', (req, res) => { 
//     res.json({msg: "Welcome"})
// })

app.listen(PORT, () => { 
    console.log('Server is running on port', PORT)
})

app.use('/user', require('./routes/userRouter'))

package.json

{
  "name": "Webapp",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "dev": "nodemon server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcrypt": "^5.0.0",
    "cloudinary": "^1.23.0",
    "concurrently": "^5.3.0",
    "cookie-parser": "^1.4.5",
    "cors": "^2.8.5",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "express-fileupload": "^1.2.1",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.11.12"
  },
  "devDependencies": {
    "nodemon": "^2.0.7"
  }
}

2 Answers

Just use localhost:5000 instead of localhost/5000 for url

Answered by simmu simu on January 28, 2021

app.listen() should be at end of file, after app.use()

Answered by Radeanu on January 28, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP