我有一臺運行在我的服務器上的node.js express服務器。我最近遷移到一個新的服務器,以前的代碼已停止工作。Node.js路由不工作node.js + express服務器
這裏是我的服務器
var fs = require('fs');
var express = require('express');
var routes = require('./routes');
var https = require('http');
var path = require('path');
var socketio = require('socket.io');
var util = require('util');
var url = require('url');
var HashMap = require('hashmap').HashMap;
var sizeOf = require('image-size');
var DBWrapper = require('node-dbi').DBWrapper;
var DBExpr = require('node-dbi').DBExpr;
var dbConnectionConfig = { host: 'localhost', user: 'mps', password: 'password', database: 'mps_schools' };
dbWrapper = new DBWrapper("pg", dbConnectionConfig);
dbWrapper.connect();
var app = express();
app.set('port', process.env.PORT || 3001);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.get('/', routes.index);
的設置在這行代碼是打破
app.get('/', routes.index);
日誌記錄 「routes.index」 來安慰,我得到 「未定義」
以下是錯誤日誌:
/home/mps/node_modules/express/lib/router/index.js:291
throw new Error(msg);
^
Error: .get() requires callback functions but got a [object Undefined]
at /home/mps/node_modules/express/lib/router/index.js:291:11
at Array.forEach (native)
at Router.route (/home/mps/node_modules/express/lib/router/index.js:287:13)
at Router.(anonymous function) [as get] (/home/mps/node_modules/express/lib/router/index.js:318:16)
at Function.app.(anonymous function) [as get] (/home/mps/node_modules/express/lib/application.js:412:26)
at Object.<anonymous> (/home/mps/app22.js:62:5)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
我做錯了什麼?謝謝
很難說,但很明顯'routes'導入失敗。可以是任何事情,但我會確保你的環境是「相同的」,你的構建過程是完整的,等等。 –
同意@DaveNewton。您的導入(require('./ routes');)失敗。發佈routes.js代碼和您的項目路徑結構。否則,很難幫助更多。 – deChristo
可能是因爲版本差異? – user2238284