[xxx] express -V 3.0.0rc1
[xxx] node -v 0.8.4
我发现有个地方已经和node.js开发指南上不一样了: 在 microblog/views/下面是 index.jade layout.jade 此时我按照书中的例子生成两个 index.ejs layout.ejs放进去。 然后将原来的jade文件改名备份,再运行node app.js 直接报错:
Express
500 Error: Cannot find module ‘jade’
at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:362:17) at require (module.js:378:17) at new View (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/view.js:42:49) at Function.app.render (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/application.js:486:12) at ServerResponse.res.render (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/response.js:676:7) at exports.index (/home/xxx/Project/nodeJs/microblog/routes/index.js:7:7) at callbacks (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/router/index.js:165:11) at param (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/router/index.js:139:11)
这个问题如何引起的?思路是什么?
Express server listening on port 3000
Error: Cannot find module ‘jade’ at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:362:17) at require (module.js:378:17) at new View (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/view.js:42:49) at Function.app.render (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/application.js:486:12) at ServerResponse.res.render (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/response.js:676:7) at exports.index (/home/xxx/Project/nodeJs/microblog/routes/index.js:7:7) at callbacks (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/router/index.js:165:11) at param (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/router/index.js:139:11) GET / 500 93ms
/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:27 var Store = connect.session.Store; ^ TypeError: Cannot read property ‘Store’ of undefined at new module.exports (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:27:30) at Function.<anonymous> (/home/xxx/Project/nodeJs/microblog/app.js:28:12) at Function.app.configure (/home/xxx/Project/nodeJs/microblog/node_modules/express/lib/application.js:399:61) at Object.<anonymous> (/home/xxx/Project/nodeJs/microblog/app.js:17:5) at Module._compile (module.js:449:26) at Object.Module._extensions…js (module.js:467:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12) at Module.runMain (module.js:492:10) at process.startup.processNextTick.process._tickCallback (node.js:244:9)
上面的问题初步解决,又有新的问题: 我已经安装了 mongoDB connect-mongo
[root@localhost microblog]# node app.js Express server listening on port 3000
/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:126 throw new Error(‘Error connecting to database’); ^ Error: Error connecting to database at module.exports.MongoStore.proto (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/lib/connect-mongo.js:126:15) at Db.open (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/db.js:253:16) at Server.connect.connectionPool.on.server._serverState (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:408:7) at EventEmitter.emit (events.js:115:20) at connection.on.connectionStatus (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:93:15) at EventEmitter.emit (events.js:91:17) at Socket.errorHandler (/home/xxx/Project/nodeJs/microblog/node_modules/connect-mongo/node_modules/mongodb/lib/mongodb/connection/connection.js:382:10) at Socket.EventEmitter.emit (events.js:88:17) at Socket._destroy.self.errorEmitted (net.js:335:14)
最新的错误, 貌似没有router这个方法。
[root@localhost microblog]# node app.js
/home/xxx/Project/nodeJs/microblog/app.js:32 app.use(express.router(routes)); ^ TypeError: Object function createApplication() { var app = connect(); utils.merge(app, proto); app.request = { proto: req }; app.response = { proto: res }; app.init(); return app; } has no method ‘router’ at Function.<anonymous> (/home/xxx/Project/nodeJs/microblog/app.js:32:19)
/**
- Module dependencies. */
var express = require(‘express’) , routes = require(‘./routes’) , http = require(‘http’) , path = require(‘path’);
var app = express(); //var app = module.exports = express.createServer();
var MongoStore = require(‘connect-mongo’)(express); var settings = require(‘./settings’);
//var app = module.exports = express.createServer(); app.configure(function(){ app.set('port’, process.env.PORT || 3000); app.set('views’, __dirname + ‘/views’); app.set('view engine’, ‘ejs’); app.use(express.favicon()); app.use(express.logger(‘dev’)); app.use(express.bodyParser()); app.use(express.methodOverride()); app.use(express.cookieParser()); app.use(express.session({ secret: settings.cookieSecret, store: new MongoStore({ db: settings.db }) })); app.use(express.router(routes)); //app.use(app.router); app.use(express.static(__dirname + ‘/public’)); });
app.configure('development’, function(){ app.use(express.errorHandler()); });
app.get('/’, routes.index); app.get('/u/:user’, routes.user); app.post('/post’, routes.post); app.get('/reg’, routes.reg); app.post('/reg’, routes.doReg); app.get('/login’, routes.login); app.post('/login’, routes.doLogin); app.get('/logout’, routes.logout);
http.createServer(app).listen(app.get(‘port’), function(){ console.log("Express server listening on port " + app.get(‘port’)); });
@saighost app.use(app.router);可以用,但是照书里写的做用了app.use(express.router(routes)); 结果报express找不到router方法了,请问这个你知道原因么?被卡住了。。。
@isondada 一个简单的解决办法是用express从新生成一个网站,将书里代码和它生成的对照看,这样起码不会被错误卡住,不明白的地方可以在这里提问。我觉得不要一上来就学习express做网站,一定要有一些扎实的js基础做前提。
var MongoStore = require(‘connect-mongodb’); 加上了 app.use(express.session({ secret: settings.cookieSecret, store: new MongoStore({ db: settings.db }) }));
就会出错,如下:
TypeError: Cannot call method ‘findOne’ of null at MongoStore.MONGOSTORE.get (D:\express\microblog\node_modules\connect-mongodb\lib\connect-mongodb.js:115:15) at Object.session [as handle] (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\session.js:298:11) at next (D:\express\microblog\node_modules\express\node_modules\connect\lib\proto.js:190:15) at Object.methodOverride [as handle] (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5) at next (D:\express\microblog\node_modules\express\node_modules\connect\lib\proto.js:190:15) at Object.cookieParser [as handle] (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\cookieParser.js:60:5) at next (D:\express\microblog\node_modules\express\node_modules\connect\lib\proto.js:190:15) at multipart (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\multipart.js:62:61) at module.exports (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:57:9) at urlencoded (D:\express\microblog\node_modules\express\node_modules\connect\lib\middleware\urlencoded.js:52:72)
怎么连接mongodb
我运行 npm install 更新包依赖的时候都出一大堆错误,晕死了,是因为我是xp系统么?
npm http GET https://registry.npmjs.org/mongodb npm http GET https://registry.npmjs.org/connect-mongo npm http GET https://registry.npmjs.org/express/2.5.8 npm http GET https://registry.npmjs.org/mongodb npm http GET https://registry.npmjs.org/connect-mongo npm http GET https://registry.npmjs.org/ejs npm ERR! Error: connect ETIMEDOUT npm ERR! at errnoException (net.js:768:11) npm ERR! at Object.afterConnect [as oncomplete] (net.js:759:19) npm ERR! If you need help, you may report this log at: npm ERR! http://github.com/isaacs/npm/issues npm ERR! or email it to: npm ERR! [email protected]
npm ERR! System Windows_NT 5.1.2600 npm ERR! command “C:\Program Files\nodejs\\node.exe” “C:\Program Files\nod ejs\node_modules\npm\bin\npm-cli.js” “install” npm ERR! cwd E:\test\Node.js\sources\chap5\sec6\microblog npm ERR! node -v v0.8.8 npm ERR! npm -v 1.1.59 npm ERR! syscall connect npm ERR! code ETIMEDOUT npm ERR! errno ETIMEDOUT npm ERR!
//继续使用app.use(app.router),在app.js最后执行一下routes(app);即可
app.configure('development', function(){
app.use(express.errorHandler());
});
routes(app);//这里执行
// routes/index.js 还按书上的来
module.exports=function(app){
app.get('/',function(req,res){
res.render('index',{
title:'首页'
});
});
};
这个问题我也纠结了好久。。哈哈仔细看了下 官方文档。。 我重新下来了 新的模块解决了。。。 mongoStore = require(‘connect-mongodb’) 从来下载 connect-mongodb 多了一个 db 不知道 他俩有啥区别 。。现在不报错了。。
我的出现了新问题:说是没有方法render
E:\nodejs\lianxi\microblog>node app.js
connect.multipart() will be removed in connect 3.0
visit https://github.com/senchalabs/connect/wiki/Connect-3.0 for alternatives
connect.limit() will be removed in connect 3.0
Express server listening on port 3000
TypeError: Object #<IncomingMessage> has no method ‘render’
at E:\nodejs\lianxi\microblog\routes\index.js:8:7
at callbacks (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:164:37)
at param (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:138:11)
at pass (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:145:5)
at Router._dispatch (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:173:5)
at Object.router (E:\nodejs\lianxi\microblog\node_modules\express\lib\router\index.js:33:10)
at next (E:\nodejs\lianxi\microblog\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at E:\nodejs\lianxi\microblog\node_modules\express\node_modules\connect\node_modules\express-session
index.js:350:9
at E:\nodejs\lianxi\microblog\node_modules\connect-mongo\lib\connect-mongo.js:222:17
at E:\nodejs\lianxi\microblog\node_modules\connect-mongo\node_modules\mongodb\lib\mongodb\collection
query.js:147:5
GET / 500 41ms