mongoose.mongo code example
Example 1: mongoose
import express from 'express';
or
const exporess = require('express');
import mongoose from 'mongoose';
or
const mongoose = require('mongoose');
const app = express();
mongoose.connect('mongodb://localhost:27017/mongo-tree' , {
useNewUrlParser: true,
useUnifiedTopology: true
});
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('connected db')
});
app.use('/', (req, res) => {
res.send('hello');
}
);
const port = process.env.PORT || 5000
app.listen(port, () => {
console.log('running');
})
{
"presets": [
"@babel/preset-env"
]
}
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon --exec babel-node server"
},
Example 2: mongoose node js
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
});