我有这段代码,它说这个错误
TypeError: app.listen is not a function
at mongoose.connect (C:\wamp\www\curso-mean2\index.js:14:7) at C:\wamp\www\curso-mean2\node_modules\mongoose\lib\connection.js:292:19 at open (C:\wamp\www\curso-mean2\node_modules\mongoose\lib\connection.js:576:17)
[nodemon] app crashed - waiting for file changes before starting...
app.js的内容:
'use strict'
var express = require('express'); // objeto express dentro de variable app
var bodyParser = require('body-parser');
var app = express();
// cargar rutas
//configurar body parser
//es necesario para body parse y convierte a objetos Json los datos que llegan por http:
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
//configurar cabeceras http
//rutas base
//exportamos el modulo , podemos utilizar express dentro de ficheros que incluyan app
module.exports = app;
index.js的内容:
'use strict'
var mongoose = require('mongoose');
var app = require('./app');
//configurar puerto por defecto
var port = process.env.PORT || 3977;
mongoose.connect('mongodb://localhost:27017/curso_mean2', (err,res) => {
if (err){
throw err;
}else{
console.log("La base de datos esta funcionando muy bien...");
//a escuchar
app.listen(port, function () {
console.log("Servidor del api rest de musica escuchando en http://localhost");
});
} //else
});
你能帮我一下吗?我是初学者,我不知道发生了什么
最佳答案
底层 MongoDB 驱动程序已弃用其当前的连接字符串解析器。因为这是一个重大更改,所以他们添加了 useNewUrlParser 标志以允许用户回退到旧的解析器,因此您应该使用类似的内容:
mongoose.connect('mongodb://localhost:27017/curso_mean2', { useNewUrlParser: true, useUnifiedTopology: true })
如果你想捕获错误,我建议你使用 Promise 和 catch 方法:
mongoose.connect('mongodb://localhost:27017/curso_mean2', { useNewUrlParser: true, useUnifiedTopology: true })
.then(()=>{
console.log("La base de datos esta funcionando muy bien...");
app.listen(port,function () {
console.log("Servidor del api rest de musica escuchando en http://localhost");
});
})
.catch( err => console.log(err) );
关于javascript - Listen 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43005872/