gpt4 book ai didi

node.js - 设置 Sequelize

转载 作者:行者123 更新时间:2023-12-03 22:42:51 25 4
gpt4 key购买 nike

我是第一次习惯于 Node 和设置数据库的编程新手。所以请指出我做错了什么。我创建了一个默认的 express 应用程序。并运行它得到这个错误。

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;

谢谢大家
问题解决了我从 sequelize 代码中添加了 http create server 并且 app.listen 在 ./bin/www 中没有注意到它是什么导致了这个错误。

最佳答案

EADDRINUSE 意味着其他东西已经在你试图绑定(bind)你的快速服务器的端口上监听。这与sequ​​elize无关

关于node.js - 设置 Sequelize ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26034974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com