【问题标题】:nodeJS - Unhandled 'error' eventnodeJS - 未处理的“错误”事件
【发布时间】:2017-11-03 04:27:06
【问题描述】:

每次启动npm start 时都会收到此错误,据我所知,这是由于同一端口上运行的进程所致,因此我必须使用netstat -nlp | grep 3000 搜索并终止该进程。

但是在每次文件更改时都这样做很不方便,我也有 nodemon 的错误可能导致它:[nodemon] app crashed - waiting for file changes before starting...

App.js:

var mongoose = require("mongoose");
var bodyParser = require("body-parser");
mongoose.Promise = require("bluebird");
var apiRouter = require('./routes/routes');
var express = require("express");
var app = express();

//raccourci pour bootstrap
app.use(bodyParser.urlencoded({
    extended: true
}));
app.use(bodyParser.json());
app.use(express.static('public'));
app.set('view engine', 'ejs');
//Ajout du slug course
app.use('/courses', apiRouter);

//Connexion à la bdd
mongoose.connect('mongodb://my_connection_string')
                .then(
                    console.log("CONNECTE !!!")
                );

app.listen(3000, function(){
    console.log("Bonjour !");
}); 

【问题讨论】:

  • 你能显示你的代码吗? (app.js 文件)
  • 完成了@Héctor

标签: node.js npm nodemon


【解决方案1】:

当你的应用崩溃时杀死猫鼬:

process.on("SIGINT", () => {
    mongoose.connection.close(() => {
        console.log("App is closing, ending mongoose connection");
        process.exit(0);
    });
});

虽然,您可能会遇到这个错误:

remy/nodemon #1025: Nodemon frequently leaves the child process running (detached)

【讨论】:

  • 老哥,这个问题我遇到过很多次了,谢谢指出!
猜你喜欢
  • 2018-12-10
  • 2019-10-20
  • 1970-01-01
  • 2018-02-11
  • 2019-11-10
  • 2020-10-10
  • 2018-12-07
  • 2016-10-13
  • 1970-01-01
相关资源
最近更新 更多