【问题标题】:ExpressJS: localhost didn’t send any dataExpressJS:本地主机没有发送任何数据
【发布时间】:2020-05-30 16:58:34
【问题描述】:

我正在编写一个应用程序,当我启动服务器时,它运行良好,它连接到数据库。但是当我从浏览器启动http://localhost:5000 时,它没有响应一分钟然后浏览器显示一条消息:

localhost didn’t send any data.
ERR_EMPTY_RESPONSE

这是我的 app.js:

const express = require('express');
const app = express();
const cookieParser = require('cookie-parser');
const mongoose = require('mongoose');
app.use(cookieParser);
app.use(express.json());



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

const startApp = async () => {
    try {
        await mongoose.connect('mongodb+srv://username:pass@cluster0-dcytp.mongodb.net/test?retryWrites=true&w=majority',
        { useUnifiedTopology: true,useNewUrlParser: true });
        console.log(`successfully connected to database`);
        const port = process.env.PORT || 5000
        app.listen(port, () => {
            console.log(`server runnin at ${port}`);
        });
    } catch (error) {
        console.log(error.message)
    }
}

startApp();

app.get('/', (req, res) => {
    console.log("I am in the root");
    res.send("hello World");
})

为什么服务器没有从浏览器响应?

【问题讨论】:

  • 如果密码是您的真实凭据,您可能需要更改密码。
  • 这些不是真正的通行证或用户。 :)
  • 您的密码仍在编辑历史记录中。 stackoverflow.com/revisions/62098528/1
  • 您能否(从日志中)确认您的服务器正在运行并且在调用端点时记录了I am in the root

标签: node.js express


【解决方案1】:

试试

app.use(cookieParser())

而不是

app.use(cookieParser)

Reference

【讨论】:

    【解决方案2】:

    我有同样的问题 db is connected 但无法发送数据。它看起来很奇怪,但对我有用。

    用新密码添加一个新的数据库用户,使用新的用户名和密码连接你的 mongoodb this is the exact like to add new database user

    这是我的数据库连接,它也可以帮助你

    mongoose.connect(DATABASE_URL, { useNewUrlParser: true,useUnifiedTopology: true  })
    const db = mongoose.connection
    db.on('error', (error) => console.error(error))
    db.once('open', () => console.log('connected to database'))
    
    app.use(express.json())
    

    【讨论】:

      猜你喜欢
      • 2019-08-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-20
      • 1970-01-01
      • 2020-09-19
      • 2018-02-16
      • 1970-01-01
      • 2011-08-08
      相关资源
      最近更新 更多