Saturday, 3 June 2017

Server Listening but not able to Connect Node

So I am trying to get started on a Node App. Right now I am just setting up my server. I am using Node, Express, Handlebars, and MongoDB. So, I have set up my server and it seems to be working fine. It is listening on Port 8080. But when I go to localhost://8080, I am getting an error message: "Localhost refused to connect."

I have tried to change my routing, etc and have had no luck fixing it. I am guessing that it is my router, or maybe how I am trying to render the page. You can see my server.js and routing file here:

var express = require("express");
var bodyParser = require("body-parser");
var mongoose = require("mongoose"); 
var methodOverride = require("method-override");



// Our scraping tools
var request = require("request");
var cheerio = require("cheerio");

// Set mongoose to leverage built in JavaScript ES6 Promises
mongoose.Promise = Promise;

// Initialize Express
var app = express();
var port = process.env.PORT || 8080;
// Use morgan and body parser with our app
// Override with POST having ?_method=DELETE
app.use(methodOverride("_method"));
app.use(bodyParser.urlencoded({
extended: false
}));

// Make public a static dir
app.use(express.static("public"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use(bodyParser.json({ type: "application/vnd.api+json" }));
// Database configuration with mongoose
mongoose.connect("mongodb://localhost/week18day3mongoose");
var db = mongoose.connection;
// Show any mongoose errors

db.on("error", function(error) {
  console.log("Mongoose Error: ", error);
});

// Once logged in to the db through mongoose, log a success message
db.once("open", function() {
  console.log("Mongoose connection successful.");
});

// Set Handlebars.
var exphbs = require("express-handlebars");
app.engine("handlebars", exphbs({ defaultLayout: "main" }));
app.set("view engine", "handlebars");



app.listen(function(){
 console.log("Server is running and lisening on port: " + port);
});


require("./routes/htmlRoutes.js");

Here is my router:

var express = require("express");


module.exports = function(app) {

   router.get("/", function(error,doc){
     res.send("Hello World");
 });

}

You can see the entire project up to this point here: https://github.com/Bentley912/Article-Scraper



via Patrick Bentley

No comments:

Post a Comment