AnswerBun.com

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

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"
  }
}

Stack Overflow Asked by Nyasha Majome on January 28, 2021

2 Answers

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!

Related Questions

form validation with data ranges

2  Asked on August 7, 2020 by eniol

   

How to format this output?

2  Asked on August 6, 2020 by shantanu-jain

     

uploading csv file django using a form

2  Asked on August 5, 2020 by sheraram_prajapat

       

Set focus to another Control

3  Asked on August 4, 2020 by binu

 

Animate gradient bar chart – matplotlib

1  Asked on August 4, 2020 by jonboy

       

c# error – Can not convert Array to byte array

1  Asked on August 2, 2020 by rakesh

         

Correcting Business name misspellings

1  Asked on August 2, 2020 by jonathan-rauscher

     

How to cut a string to the first “/” from right to left c# .net

1  Asked on August 1, 2020 by ignacio-gomez

   

Inserting random data from a list

2  Asked on August 1, 2020 by smiley

     

Invalid token SELECT

2  Asked on July 31, 2020 by virendra-varma

     

Django – No column found for custom field?

0  Asked on July 29, 2020 by jare42

       

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved.