登录
注册
node.js 学习社区
node搭建第一个microblog的时候,app.js中只要加入链接mongodb的代码就无法启动

bais0326

2016-06-04 15:45

app.js 中的代码如下:


var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var flash = require('connect-flash');
var session = require('express-session');
var Mongostore = require('connect-mongo')(session);

var routes = require('./routes/index');
var settings = require('./settings');
var users = require('./routes/users');
var post = require('./routes/post');
var reg = require('./routes/reg');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// app.use(express.cookieParser());
app.use(express.session({
  secret:settings.cookieSecret,
  store:new Mongostore({
    db:settings.db;
  });
  // store: new MongoStore({host:settings.host, port:settings.port, db:settings.db)})
}));
app.use(flash());


app.use('/', routes);
app.use('/users', users);
app.use('/post', post);  

app.use('/reg', reg);

module.exports = app;

运行cnpm start 的时候报错:



D:\BaiduYunDownload\Nodejs\microblog>cnpm start

> microblog@0.0.0 start D:\BaiduYunDownload\Nodejs\microblog
> node ./bin/www

D:\BaiduYunDownload\Nodejs\microblog\app.js:35
  store: new MongoStore({host:settings.host, port:settings.port, db:settings.db)
})
                                                                               ^


SyntaxError: Unexpected token )
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Module.require (module.js:353:17)
    at require (internal/module.js:12:17)
    at Object.<anonymous> (D:\BaiduYunDownload\Nodejs\microblog\bin\www:7:11)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)

npm ERR! Windows_NT 6.1.7601
npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "D:\\npmConfig\\npm\\node_mo
dules\\cnpm\\node_modules\\npm\\bin\\npm-cli.js" "--userconfig=C:\\Users\\Admini
strator\\.cnpmrc" "--disturl=https://npm.taobao.org/mirrors/node" "--registry=ht
tps://registry.npm.taobao.org" "start"
npm ERR! node v4.4.5
npm ERR! npm  v3.9.5
npm ERR! code ELIFECYCLE
npm ERR! microblog@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the microblog@0.0.0 start script 'node ./bin/www'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the microblog package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs microblog
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls microblog
npm ERR! There is likely additional logging output above.

npm ERR! Please include the following file with any support request:
npm ERR!     D:\BaiduYunDownload\Nodejs\microblog\npm-debug.log
但是如果不加入app.js 中的



app.use(express.cookieParser());
app.use(express.session({
  secret:settings.cookieSecret,
  store:new Mongostore({
    db:settings.db;
  });
  // store: new MongoStore({host:settings.host, port:settings.port, db:settings.db)})
}));
这部分代码就可以运行成功。


settings.js 中的代码如下:


module.exports = {
	cookieSecret: 'microblogbs',
	db: 'microblog',
	host: 'localhost',
	port: 27017
}
数据库db.js 如下:



var settings = require('../settings');
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;

// module.exports=new Db(settings.db,new Server(settings.host,Connection.DEFAULT_PORT,{}));
module.exports = new Db(settings.db, new Server(settings.host, settings.port),
 {safe: true});
express版本是4.x版本,捣鼓了一天了实在没弄清楚怎么回事,小弟新手,求各位大神帮帮忙,指导指导,感激不尽啊!


回复 · 2

  • app.use(cookieParser());
    app.use(session({
    secret: setting.cookieSecret,
    key: setting.db,
    cookie: {
    maxAge: 1000 * 60 * 60 * 24
    },
    resave: false,
    saveUninitialized: true,
    store: new MongoStore({ mongooseConnection: mongoose.connection })
    }))

    0

  • module.exports = { cookieSecret:"sadsfdsds", mongodb: 'mongodb://127.0.0.1:27017/test' };

    0

发表回复

你可以在回复中 @ 其他人