本文整理匯總了TypeScript中mongoose.connect函數的典型用法代碼示例。如果您正苦於以下問題:TypeScript connect函數的具體用法?TypeScript connect怎麽用?TypeScript connect使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了connect函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
import * as mongoose from "mongoose";
import {Connection} from "mongoose";
import {options} from "../helpers/key";
mongoose.connect("mongodb://ds064188.mlab.com:64188/walter", options);
let db: Connection = mongoose.connection;
db.on("error", console.error.bind(console, "connection error:"));
db.once("open", () => {
console.log("we are connected");
});
export const conn: Connection = db;
示例2: require
// Imports
import * as mongoose from "mongoose";
import * as express from "express";
import * as Order from './api/orders/order.model'
import config = require('./config/config');
//Global Vars
const PORT = process.env.PORT || 3000;
const MONGO_URL = process.env.MONGO_URL || "mongodb://localhost/container-manager";
let app = express();
//Mongoose Connection
require('./api/orders/order.model');
require('./api/users/user.model');
mongoose.connect(MONGO_URL, function(err){
if(err) console.log(err)
else console.log(`connected to ${MONGO_URL}`)})
app.get(/\/client.{0,}\/.+\.jade/, (req, res, next) => {
res.render(config.root + req.path);
})
//Routes config
app.use(require('body-parser')());
// access bower_components via /scripts/...
app.use('/bower_components', express.static('bower_components'));
// access the client->app->home folder via /app/home
app.use('/client', express.static('client'));
// Routes
app.get('/', (req, res, next) => {
示例3: require
kernel.bind<ParkingRepository>(TYPES.ParkingRepository).to(ParkingRepositoryBasic).inSingletonScope();
kernel.bind<UserRepository>(TYPES.UserRepository).to(UserRepositoryBasic).inSingletonScope();
kernel.bind<TrackingRepository>(TYPES.TrackingRepository).to(TrackingRepositoryBasic).inSingletonScope();
kernel.bind<GoogleDistanceMatrixKey>(TYPES.GoogleDistanceMatrixKey).to(GoogleDistanceMatrixKeyBasic).inSingletonScope();
// some variables
let version = '0.1.3';
let logger: Logger = kernel.get<Logger>(TYPES.Logger);
let parkingService: ParkingService = kernel.get<ParkingService>(TYPES.ParkingService);
let testDataService: TestDataService = kernel.get<TestDataService>(TYPES.TestDataService);
let userService: UserService = kernel.get<UserService>(TYPES.UserService);
let trackingService: TrackingService = kernel.get<TrackingService>(TYPES.TrackingService);
// mongodb (with mongoose) connection
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
mongoose.connection.on('error', console.error.bind(console, 'connection error:'));
var serverError = function (response: Response, message: string, user?: User) {
logger.error(message, user);
response.type('application/json; charset=utf-8');
response.writeHead(500);
response.write('{"error": "' + message + '"}');
response.end();
};
let app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
示例4: require
import express = require('express');
import path = require('path');
var port: number = 8080;
var app = express();
var compression = require('compression');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
import {TimeLog} from "./models/timelog.mongoose.model";
mongoose.connect('mongodb://localhost/angular-test');
app.use(compression());
app.use(bodyParser.urlencoded({'extended': 'true'})); // parse application/x-www-form-urlencoded
app.use(bodyParser.json());
app.use(express.static(path.resolve(__dirname, '../client/dist')));
var renderIndex = (req: express.Request, res: express.Response) => {
res.sendFile(path.resolve(__dirname, '../client/dist/app.html'));
};
app.get('/api/timelogs', function (req, res) {
TimeLog.find(function (err, timeLogs) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err) {
res.send(err);
}
res.json({data:timeLogs}); // return all timelogs in JSON format
});
});
示例5: require
/// <reference path="typings/express/express.d.ts" />
/// <reference path="typings/node/node.d.ts" />
import express = require('express');
import path = require('path');
import routes = require('./routes/routes');
var configDB = require('./config/database');
var mongoose = require('mongoose');
//TODO : Must be possible to type better
mongoose.connect(configDB.url); // connect to our database
var favicon = require('static-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var passport = require('passport');
var session = require('express-session')
var flash = require('express-flash');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon());
示例6: require
/// <reference path='./app.d.ts' />
// enable source transformation
var asyncblock = require('asyncblock');
asyncblock.enableTransform();
// start our mongo connection
import config = require('./config');
var mongoose = require('mongoose');
mongoose.connect(config.props.mongo.uri);
// create our web server
import express = require('express');
export var app = express();
app.set('port', config.props.server.port);
app.use(express.bodyParser());
app.use(express.methodOverride());
// define our controllers
app.use(app.router);
require('./controllers')(app);
// register a global error handler
app.use((err, req, res, next) => {
switch (err.name) {
case 'ValidationError':
// mongoose validation errors
res.send(400, { error: err.toString() });
break;
default:
// unknown error type