1import express from 'express';
2or
3const exporess = require('express');
4import mongoose from 'mongoose';
5or
6const mongoose = require('mongoose');
7const app = express();
8mongoose.connect('mongodb://localhost:27017/mongo-tree' , {
9 useNewUrlParser: true,
10 useUnifiedTopology: true
11});
12
13const db = mongoose.connection;
14db.on('error', console.error.bind(console, 'connection error:'));
15db.once('open', function() {
16console.log('connected db')
17});
18app.use('/', (req, res) => {
19 res.send('hello');
20}
21);
22const port = process.env.PORT || 5000
23app.listen(port, () => {
24console.log('running');
25})
26//for es6 >npm i @babel/core @babel/node @babel/preset-env --save-dev
27
28//create .babelrc file and put this
29{
30 "presets": [
31 "@babel/preset-env"
32 ]
33}
34//and package.json
35 "scripts": {
36 "test": "echo \"Error: no test specified\" && exit 1",
37 "start": "nodemon --exec babel-node server"
38 },
1Mongoose is a cross-platform embedded web server and networking library.
2The small footprint of the software enables any
3Internet-connected device to function as a web server.
1// getting-started.js
2var mongoose = require('mongoose');
3mongoose.connect('mongodb://localhost/test', {useNewUrlParser: true});
1MongoDB is a general purpose,
2document-based,
3distributed database
4built for modern application developers and for the cloud era.
5
6No database makes you more productive.
7
1const mongoose = require('mongoose');
2mongoose.connect('mongodb://localhost:27017/test', {useNewUrlParser: true, useUnifiedTopology: true});
3
4const Cat = mongoose.model('Cat', { name: String });
5
6const kitty = new Cat({ name: 'Zildjian' });
7kitty.save().then(() => console.log('meow'));
1var db = mongoose.connection;
2db.on('error', console.error.bind(console, 'connection error:'));
3db.once('open', function() {
4 // we're connected!
5});