简体   繁体   English

Node.js服务器在端口3000上运行并在侦听,但Localhost:3000没有发送任何数据

[英]Node.js server running and listening on Port 3000 but Localhost:3000 didn't send any data

So the server is running and listening to port 3000 which isn't being blocked as far as I can tell, but when I try and load the page it hangs for a while before providing the following message: 因此,服务器正在运行并正在侦听3000端口,据我所知,该端口并未被阻塞,但是当我尝试加载页面时,它会挂起一段时间,然后提供以下消息:

"This page isn't working localhost didn't send any data. ERR_EMPTY_RESPONSE “此页面无法正常运行,localhost没有发送任何数据。ERR_EMPTY_RESPONSE

I'm not sure why I can't get anything to pull through even when I adjust the get('/") homepage to render something simple. There also aren't any errors pulling through the terminal so I'm unsure how to diagnose the problem. 我不确定为什么即使调整get('/“)主页以使内容变得简单也无法通过,也没有任何错误通过终端,所以我不确定如何诊断问题。

My app.js code: 我的app.js代码:

const express = require('express');
const path = require('path');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const expressValidator = require('express-validator');
const flash = require('connect-flash');
const session = require('express-session');


mongoose.connect('mongodb://localhost/nodekb');
let db = mongoose.connection;

// Check connection
db.once('open', function(){
  console.log('Connected to MongoDB');
});

// Check for DB errors
db.on('error', function(err){
  console.log(err);
});

// Init App
const app = express();

// Bring in Models
let Article = require('./models/article');

// Load View Engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

// Body Parser Middleware
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());

// Set Public Folder
app.use(express.static(path.join(__dirname, 'public')));

// Express Session Middleware
app.use(session({
  secret: 'keyboard cat',
  resave: true,
  saveUninitialized: true
}));

// Express Messages Middleware
app.use(require('connect-flash')());
app.use(function (req, res, next) {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

// Express Validator Middleware
app.use(expressValidator);

// Home Route
app.get('/', function(req, res){
  Article.find({}, function(err, articles){
    if(err){
      console.log(err);
    } else {
      res.render('index', {
        title:'Articles',
        articles: articles
      });
    }
  });
}); 

// Route Files
let articles = require('./routes/articles');
let users = require('./routes/users');
app.use('/articles', articles);
app.use('/users', users);

app.listen(3000, function(){
  console.log("We are running on port 3000");
});

I think the problem is your query is fetching empty array . 我认为问题是您的查询正在获取空数组 I have added condition in callback of query to check array length of articles . 我在查询的回调添加条件检查的物品 数组的长度

app.get('/', function(req, res){
    Article.find({}, function(err, articles){
          if(err){
              console.log(err);
              res.json(err);
          }else if(articles.length == 0){
              console.log("Articles not found");
              res.json({error : "Articles not found"});
          } else {
              console.log("Articles found");
              res.render('index', {
                  title:'Articles',
                  articles: articles
              });
          }
      });
}); 

I hope it will help you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js 端口 3000 已在使用但实际上不是? - Node.js Port 3000 already in use but it actually isn't? Node.js localhost:3000 拒绝连接 - Node.js localhost:3000 refuses to connect Express服务器未监听本地主机上的特定端口3000 [处于保留状态] - Express server not listening to specific port 3000 on localhost [on hold] 表示不监听localhost:3000 - Express not listening on localhost:3000 在node.js中,控制台显示成功日志,但是localhost:3000未连接 - In node.js, the console displays the success log, but localhost: 3000 is not connected Node.JS:为什么我与 localhost:3000 的连接被拒绝? - Node.JS: Why is my connection to localhost:3000 refused? 在3000端口上带有https的Node.js socket.io - Node.js socket.io with https on 3000 port 切换node.js应用程序时localhost:3000不变 - localhost:3000 doesn't change when switching node.js apps 尝试从 localhost:8080 向 localhost:3000 发出 GET 请求,但我收到 cors 错误,并在我的 node.js 服务器上安装 cors 无法修复它 - Trying to make a GET request from localhost:8080 to localhost:3000 but I get a cors error, and installing cors on my node.js server doesn't fix it GET http://localhost:3000/search 404 (Not Found)REACT.JS, Node.js 和 Axios - GET http://localhost:3000/search 404 (Not Found)REACT.JS, Node.js and Axios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM