Home > Net >  express cant be shown in localhost
express cant be shown in localhost

Time:12-30

I am a beginner to express,but in have diffculities when linking with the localhost in my internet.But it works fine a minute ago, it shows no error and with fine console log,but the website just stuck on the url without any error.I am wondering is the bug occur on my script or my internet

my code:

const bodyParser = require('body-parser')
const express = require('express')
const app = express()
console.log("ff")
const mongoose=require('mongoose')  
mongoose.set('strictQuery',false)
require('dotenv/config')



const db=mongoose.connection
db.on('error',(error)=>console.error(error))
db.once('open',()=> console.log('connect to db'))

mongoose.connect(
    process.env.DB_CONNECTION
    ,{ useNewUrlParser:true })
app.use(logger)
app.use(bodyParser.json)
app.get('/',logger,(req,res)=>{
    console.log("ddd")
    //res.status(500)
    res.json({ username : "alex"})

})

const userRouter = require('./routes/users')

function logger(req,res,next){
    console.log(req.originalUrl)
    next()
}
app.use('/users',userRouter)
app.listen(3000)

enter image description here

CodePudding user response:

I think the bodyParser is the problem.

I changed it and now your code works for me

use this:

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json()); //<- () missing

  • Related