How to use the mongoose.connect function in mongoose

To help you get started, we’ve selected a few mongoose examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github sslover / node-express-mongodb-heroku-boilerplate / app.js View on Github external
//  templates directory to 'views'
app.set('views', __dirname + '/views');

// setup template engine - we're using Hogan-Express
app.set('view engine', 'html');
app.set('layout','layout');
app.engine('html', require('hogan-express')); // https://github.com/vol4ok/hogan-express

app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(methodOverride());
app.use(express.static(path.join(__dirname, 'public')));

// connecting to database
app.db = mongoose.connect(process.env.MONGOLAB_URI);
console.log("connected to database");

// ROUTES, logic is in routes/index.js

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

// home page
app.get('/', routes.index); // calls index function in /routes/index.js

//add new food routes
app.get('/add',routes.addFoodForm); //display form to add a new food product
app.post('/add',routes.addFoodToDb); //form POST submits here

// display a single food item
// for example '/food/chunky-peanut-butter'
app.get('/food/:slug', routes.oneFood);
github chiefsmurph / robinhood-playground / run.js View on Github external
const login = require('./rh-actions/login');
const getTrendBreakdowns = require('./app-actions/get-trend-breakdowns');

const mongoose = require('mongoose');
const { mongoConnectionString } = require('./config');

const Pick = require('./models/Pick');

mongoose.connect(mongoConnectionString, { useNewUrlParser: true });


// node run [filename goes here]
require('./utils/fix-locale-date-string');

(async () => {
    console.log('asd')
    console.log(process.argv, 'ps');
    let Robinhood = await login();
    global.Robinhood = Robinhood;
    const argPath = process.argv[2];
    let relatedFile = require(`./${argPath}`);

    let callArgs = [];
    const restArgs = process.argv.slice(3)
        .map(arg => arg === 'true' ? true : arg)
github accimeesterlin / mern-stack-deploy-heroku / server.js View on Github external
const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');

// importing files
const routes = require('./routes');

// Define Global Variables
const app = express();
const log = console.log;
const PORT = process.env.PORT || 8080; // Step 1


// Step 2
mongoose.connect( process.env.MONGODB_URI || 'mongodb://localhost/my_database', {
    useNewUrlParser: true
});

// Configuration
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', routes);

// Step 3
if (process.env.NODE_ENV === 'production') {
    app.use(express.static( 'client/build' ));

    app.get('*', (req, res) => {
        res.sendFile(path.join(__dirname, 'client', 'build', 'index.html')); // relative path
    });
}
github littlewin-wang / littlewin.server / db / mongodb.js View on Github external
exports.connect = () => {
  mongoose.connect(dbConfig.mongo.uri)

  // 连接成功
  mongoose.connection.on('connected', () => {
    console.log('Mongoose connection open to ' + dbConfig.mongo.uri)
  })

// 连接失败
  mongoose.connection.on('error', (err) => {
    console.log('Mongoose connection error: ' + err)
  })

// 断开连接
  mongoose.connection.on('disconnected', () => {
    console.log('Mongoose connection disconnected')
  })
github rationalappdev / MovieTicketsBackend / server.js View on Github external
import express from 'express';
import morgan from 'morgan';
import mongoose from 'mongoose';
import router from './router';

// Connect to MongoDB
mongoose.connect('mongodb://localhost/movies');

// Initialize http server
const app = express();

// Logger that outputs all requests into the console
app.use(morgan('combined'));
// Use v1 as prefix for all API endpoints
app.use('/v1', router);

const server = app.listen(3000, () => {
  const { address, port } = server.address();
  console.log(`Listening at http://${address}:${port}`);
});
github iotexproject / iotex-explorer / src / server / gateways / gateways.ts View on Github external
export function setGateways(server: MyServer): void {
  server.gateways = server.gateways || {};

  if (
    // @ts-ignore
    !(server.config.gateways.mongoose && server.config.gateways.mongoose.uri)
  ) {
    server.logger.warn(
      "cannot start server without gateways.mongoose.uri provided in configuration"
    );
  } else {
    // @ts-ignore
    mongoose.connect(server.config.gateways.mongoose.uri).catch(err => {
      server.logger.warn(`failed to connect mongoose: ${err}`);
    });
  }
  // @ts-ignore
  server.gateways.mongoose = mongoose;

  const gateways = server.config.gateways;
  server.gateways.antenna = new RpcMethod(gateways.iotexAntenna);
  server.gateways.coinmarketcap = { fetchCoinPrice };
  server.gateways.sendgrid = getSendgrid(gateways.sendgrid);
}
github nodeca / nodeca.core / lib / autoload / hooks / init / models / mongoose.js View on Github external
N.logger.info('Connecting to MongoDB');

    var options = {
      promiseLibrary: require('bluebird'),
      poolSize: 10,
      connectTimeoutMS: 30000,
      keepAlive: 1,

      // fix deprecation warnings appearing in mongodb driver,
      // see https://mongoosejs.com/docs/deprecations.html for details
      useNewUrlParser: true,
      useFindAndModify: false,
      useCreateIndex: true
    };

    return mongoose.connect(config, options)
      .then(function () {
        N.logger.info('MongoDB connected');
      });
  });
};
github xcatliu / mazimd / models / index.js View on Github external
const mongoose = require('mongoose');
const config = require('../config');

mongoose.Promise = global.Promise;
mongoose.connect(config.db, (err) => {
  if (err) {
    console.error(`connect to ${config.db} error: ${err.message}`);
    process.exit(1);
  }
});

exports.Page = require('./page');