-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
48 lines (36 loc) · 1.33 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
const express = require('express');
const bodyParser = require('body-parser');
// const MongoClient = require('mongodb').MongoClient;
const mongoose = require('mongoose');
const app = express();
app.use(express.static(__dirname + '/public'));
app.set('view engine', 'ejs');
const product = require('./routes/product.route'); // Imports routes for the products
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
// Set up mongoose connection
const uri = "mongodb+srv://<dbuser>:<password>@<clustername>-dx50k.mongodb.net/<dbname>?retryWrites=true";
let mongoDB = process.env.MONGODB_URI || uri;
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'MongoDB connection error:'));
app.use('/products', product);
app.listen(3000, function() {
console.log('server running on port 3000')
});
// Set up mongodb connection
// const client = new MongoClient(uri, { useNewUrlParser: true });
// client.connect(err => {
// if(err) return console.log(err);
// db = client.db("crudnode")
// app.listen(3000, function() {
// console.log('server running on port 3000')
// });
// });
app.get('/products', (req, res) => {
res.render('index.ejs', {data:''});
});
app.get('/', (req, res) => {
res.redirect('/products');
});