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:

"This page isn’t working localhost didn’t send any data. 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.

My app.js code:

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

1 answer

  • answered 2018-04-17 05:12 Saurabh Lende

    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.