为什么我的应用程序挂在localhost上?

时间:2017-06-13 23:40:07

标签: javascript json node.js server-side serverside-javascript

我最近在NodeJS和Express中构建了一个MVC(好吧,更像是一个VC应用程序)应用程序。一切都工作正常,直到我安装了express-validator并将中间件粘贴到app文件中。之后,localhost开始挂起,并在控制台中显示GET / - - ms - - 消息。我启动了一个新的应用程序,重新安装了模块,并复制并粘贴了代码。我仍然有同样的问题,所以我删除了express-validator中间件。没有改变。  App.js(切入点):

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

路线文件(/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);
};

配置模块(/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);
};

家庭控制器(/controllers/home.js):

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

包文件(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": {}
}

当然,我在/ views目录中有一个视图文件(home.ejs)。如果您需要查看它,请告诉我,我会将其添加到帖子中。请不要将此作为副本关闭;我已经检查了类似的问题,他们主要关注没有路由器的简单应用程序或类似的东西我尝试了适用于我的解决方案,但没有一个是相关的或有效的。 更新:我在包文件中确实有特定版本,但我仍有同样的问题。

1 个答案:

答案 0 :(得分:0)

尝试在依赖项中使用特定版本(最新)的单个包。有关详细信息,请参阅 - https://docs.npmjs.com/files/package.json