简体   繁体   中英

Why is my app hanging on localhost?

I have recently built an MVC (well, more like a VC app) app in NodeJS and Express. Everything was working fine until I installed express-validator and pasted the middleware in the app file. Afterwards, localhost began hanging, with a GET / - - ms - - message in the console. I started a new app, reinstalled the modules, and copied and pasted the code. I still had the same issue, so I removed the express-validator middleware. Nothing changed. App.js (entry point):

var config = require('./server/configure');
var express = require('express');
var app = express();
var app = config(app); 
app.set('port', process.env.port || 3300);
app.set('views', __dirname + '/views');
app.listen(app.get('port'), function(req, res){
    console.log('Server up: http://localhost:' + app.get('port'));
});

The routes file (/server/routes.js)

var express = require('express');
    home = require('../controllers/home');
module.exports = function(app) {
    router = express.Router();
    router.get('/', home.home);
    app.use(router);
};

The configure module (/server/configure.js)

var path = require('path'),
    routes = require('./routes'),
    ejs = require('ejs'),
    express = require('express'),
    bodyParser = require('body-parser'),
    cookieParser = require('cookie-parser'),
    morgan = require('morgan'),
    methodOverride = require('method-override'),
    errorHandler = require('errorhandler');
module.exports = function(app) {
  app.use(morgan('dev'));
  app.use(bodyParser.json);
  app.use(bodyParser.urlencoded({extended:false}));
  app.use(bodyParser({
    uploadDir: path.join(__dirname, 'public/upload/temp')
  }));
  app.use(methodOverride());
  app.use(cookieParser('secret value'));
  routes(app);
  app.use('/public/', express.static(path.join(__dirname, '../public')));
  if ('development' === app.get('env')) {
    app.use(errorHandler());
  }
  app.set('view engine', 'ejs');
  return(app);
};

The home controller (/controllers/home.js):

module.exports = {
    home: function(req, res) {
        res.render('home');
    }
};

The Package file (package.json):

{
  "name": "krementcookdev",
  "version": "1.0.0",
  "description": "the krementcook web application",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Isaac Krementsov",
  "license": "ISC",
  "dependencies": {
    "body-parser": "*",
    "cookie-parser": "*",
    "ejs": "*",
    "errorhandler": "*",
    "express": "*",
    "express-handlebars": "*",
    "express-validator": "*",
    "method-override": "*",
    "morgan": "*",
    "path": "*"
  },
  "devDependencies": {}
}

Of course, I have a view file (home.ejs) in the /views directory. If you need to see it, let me know and I will add it to the post. Please do not close this a being a duplicate; I have checked similar problems and they mostly regard simple apps without routers or anything like that. I tried the solutions offered that applied to me, but none were relevant or productive. Update: I did have specific versions in the package file, but I still had the same issue.

Try to use specific version (latest) of individual package in dependencies. for more detail Refer - https://docs.npmjs.com/files/package.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM