设置Sequelize

时间:2014-09-25 09:25:29

标签: node.js sequelize.js

我是程序员习惯第一次习惯节点和设置数据库的新手。所以请给我一些关于我做错的指示。我创建了一个默认的快递应用程序。并运行它得到这个错误。

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EADDRINUSE
    at errnoException (net.js:904:11)
    at Server._listen2 (net.js:1042:14)
    at listen (net.js:1064:10)
    at Server.listen (net.js:1138:5)
    at Function.module.exports (D:\Developer\DurexData\app.js:69:30)
    at null.<anonymous> (D:\Developer\DurexData\node_modules\sequelize\lib\emitt
ers\custom-event-emitter.js:105:15)
    at emit (events.js:95:17)
    at module.exports.CustomEventEmitter.emit (D:\Developer\DurexData\node_modul
es\sequelize\lib\emitters\custom-event-emitter.js:61:33)
    at module.exports.finish (D:\Developer\DurexData\node_modules\sequelize\lib\
query-chainer.js:142:30)
    at exec [as fct] (D:\Developer\DurexData\node_modules\sequelize\lib\query-ch
ainer.js:96:16)

这是该计划 的 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 routes = require('./routes/index');
var users = require('./routes/users');
var tasks = require('./routes/task');
var http = require('http')
    ,   db = require('./models')

var app = express();

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

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('/', routes);
app.use('/users', users);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});

// error handlers

// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}

// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

db
  .sequelize
  .sync({ force: true })
  .complete(function(err) {
    if (err) {
      throw err[0]
    } else {
      http.createServer(app).listen(app.get('port'), function(){
        console.log('Express server listening on port ' + app.get('port'))
      })
    }
  })

module.exports = app;

模型/ index.js

var fs        = require('fs')
  , path      = require('path')
  , Sequelize = require('sequelize')
  , lodash    = require('lodash')
  , config    = require('../config/db_config')
  , sequelize = new Sequelize(config.database, config.username, config.password, {
    database: config.database, 
    username: config.username, 
    password: config.password,
    host: config.host,
    port: config.port,
    dialect: 'mysql'
  })
  , db        = {};

fs
  .readdirSync(__dirname)
  .filter(function(file) {
    return (file.indexOf('.') !== 0) && (file !== 'index.js')
  })
  .forEach(function(file) {
    var model = sequelize.import(path.join(__dirname, file))
    db[model.name] = model
  });

Object.keys(db).forEach(function(modelName) {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db)
  }
});

module.exports = lodash.extend({
  sequelize: sequelize,
  Sequelize: Sequelize
}, db);

路由/ index.js

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res) {
  res.render('index', { title: 'Express' });
});

module.exports = router;

路由/ user.js的

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res) {
  res.send('respond with a resource');
});

module.exports = router;

谢谢大家 问题解决了我从续集代码添加了http创建服务器,并且./bin/www中的app.listen没有注意到它是出现此错误的原因。

1 个答案:

答案 0 :(得分:0)

EADDRINUSE表示其他人已在侦听您尝试绑定快速服务器的端口。这与sequ​​elize

无关