gpt4 book ai didi

node.js - 如何修复 "Error: listen EADDRINUSE: address already in use:::5000"未处理的 'error' 事件

转载 作者:搜寻专家 更新时间:2023-10-31 23:31:08 24 4
gpt4 key购买 nike

我创建了一个 nodejs 服务器(一个 loginapp)但是当我尝试 Node 应用程序时出现错误(她之前不在这里):

events.js:167
throw er; // Unhandled 'error' event
^

Error: listen EADDRINUSE: address already in use :::5000
at Server.setupListenHandle [as _listen2] (net.js:1290:14)
at listenInCluster (net.js:1338:12)
at Server.listen (net.js:1425:7)
at Function.listen (C:\Users\Corentin\Documents\loginapp\node_modules\express\lib\application.js:618:24)
at Object.<anonymous> (C:\Users\Corentin\Documents\loginapp\app.js:81:5)
at Module._compile (internal/modules/cjs/loader.js:689:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
at Module.load (internal/modules/cjs/loader.js:599:32)
at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
at Function.Module._load (internal/modules/cjs/loader.js:530:3)
at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
Emitted 'error' event at:
at emitErrorNT (net.js:1317:8)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

我认为有一个与 mongo 的链接,但我没有看到错误,

这是我的 app.js 代码(不完整)问我是否需要我的更多代码

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var exphbs = require('express-handlebars');
var expressValidator = require('express-validator');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local'),Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/loginapp', { useNewUrlParser: true });
var db = mongoose.connection;

var routes = require('./routes/index');
var users = require('./routes/users');

// APP INIT
var app = express();

// VIEW ENGINE
app.set('views', path.join(__dirname,'views'));
app.engine('handlebars', exphbs({ defaultLayout:'layout' }));
app.set('view engine','handlebars');

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

// SET PORT
app.set('port', (5000));

app.listen(app.get('port'), () => {
console.log('Server lancé sur le port ' + app.get('port'));
});

这是我的 users.js 文件

var express = require('express');
var router = express.Router();
var bodyParser = require('body-parser');

var User = require('../models/user');

// REGISTER
router.get('/register', (req, res) => {
res.render('register');
});

// LOGIN
router.get('/login', (req, res) => {
res.render('login');
});

// REGISTER
router.post('/register', (req, res) => {
var name = req.body.name;
var username = req.body.username;
var email = req.body.email;
var password = req.body.password;
var password2 = req.body.password2;

// VALIDATION
req.checkBody('name','Name is required').notEmpty();
req.checkBody('username','Username is required').notEmpty();
req.checkBody('email','Email is required').notEmpty();
req.checkBody('email','Email is not valid').isEmail();
req.checkBody('password','Password is required').notEmpty();
req.checkBody('password2','Passwords do not match').equals(req.body.password);

var errors = req.validationErrors();

if(errors) {
res.render('register', {
errors:errors
});
} else {
var newUser = new User({
name: name,
username: username,
email: email,
password: password
});

User.createUser(newUser, function(err, user) {
if(err) throw err;
console.log(user);
});

req.flash('success_msg', 'You are now registered and u can log');

res.redirect('/users/login');
}
});

module.exports = router;

和 user.js 文件

var mongoose = require('mongoose');
var bcrypt = require('bcryptjs');

// USER SCHEMA
var UserSchema = mongoose.Schema({
username: {
type: String,
index: true
},
name: {
type: String
},
email: {
type: String
},
password: {
type: String
}
});

var User = module.exports = mongoose.model('User', UserSchema);

module.exports.createUser = function(newUser, callback) {
bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(newUser.password, salt, function(err, hash) {
newUser.password = hash;
newUser.save(callback);
});
});
}

非常感谢能帮助我的人!

最佳答案

试试这个

sudo lsof -i :5000

命令 PID 用户 FD 类型设备大小/关闭 Node 名称
Node 20152 abc 21u IPv6 195004 0t0 TCP *:http (LISTEN)

sudo kill -9 20152

关于node.js - 如何修复 "Error: listen EADDRINUSE: address already in use:::5000"未处理的 'error' 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54468097/

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