我是Node.js的新手,正在尝试使用TypeScript构建一个基于node/express/mongoose的服务器应用程序。
这是我的app.ts文件:
// lib/app.ts
import express from 'express';
import * as bodyParser from 'body-parser';
import { Routes } from './routes/crmRoutes';
import * as mongoose from "mongoose";
class App {
public app: express.Application;
public routePrv: Routes = new Routes();
public mongoUrl: string = 'mongodb://localhost/TodosDB';
constructor() {
this.app = express();
this.config();
this.routePrv.routes(this.app);
this.mongoSetup();
}
private mongoSetup(): void {
mongoose.connect(this.mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
});
}
private config(): void {
// support application/json type post data
this.app.use(bodyParser.json());
//support application/x-www-form-urlencoded post data
this.app.use(bodyParser.urlencoded({ extended: false }));
}
}
export default new App().app;
然而,当我尝试编译我的应用程序时,我收到以下错误信息:
TypeError: mongoose.connect is not a function
我已经尽力搜索谷歌了,但没有任何运气。
有人能告诉我我做错了什么吗?
import * as express from 'express';
,它就简单地运行了。 - Alok Deshwaltsc ./lib/server.js
进行编译。然后它成功地运行了。感谢您的帮助。 - Nick Hodges