-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
79 lines (66 loc) · 2.45 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/* Imports */
const express = require('express');
const bodyParser = require('body-parser');
const helmet = require('helmet');
const cors = require('cors');
const config = require('./config');
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
/* Import Routes */
const user = require('./routes/user');
const authentication = require('./routes/authentication');
const carts = require('./routes/cart');
const product = require('./routes/product');
const category = require('./routes/category');
/* Import Schemas */
const { User } = require('./schemas/user');
const { Product } = require('./schemas/product')
/* Initialization */
const app = express();
app.use(bodyParser.json({ limit: '50mb' }));
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true }));
app.use(helmet());
app.use(cors({
"origin": "*",
"methods": "GET,HEAD,PUT,PATCH,POST,DELETE",
"preflightContinue": false,
"optionsSuccessStatus": 200,
"credentials": true
}));
/* Routes */
app.use('/user', user);
app.use('/auth', authentication);
app.use('/cart', carts);
app.use('/product', product);
app.use('/category', category);
/* Connection MongoDB */
mongoose.connect(config.mongo_db_uri, { useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true, useUnifiedTopology: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'Failed connection.'));
db.once('open', function () {
console.log("Successfully connection.")
});
/* Server */
const port = config.port;
app.listen(port, () => {
console.log('The server is running on ' + config.port + ' ...');
});
/* Initialize Admin */
User.find({}).countDocuments().then(async (count) => {
if (count === 0) {
let password = await bcrypt.hash('password', 10);
const user = new User({ first_name: 'Origen', last_name: 'Challenge v3', email: '[email protected]', password: password, roles: ['Administrator'] });
user.save();
console.log("User: " + JSON.stringify(user) + " had been created successfully.")
}
});
/* Inizialize Products*/
Product.find({}).countDocuments().then(async (count) => {
if (count === 0) {
product1 = new Product({ name: "RTX 3080 Ti", model: "Gigabyte", categoty: "hardware", price: 1 });
product1.save();
}
});
//cart = new Cart({ products: [{ name: "RTX 3080 Ti", model: "Gigabyte", categoty: "hardware", price: 1 }], colors: [{ user: "patata", color: "green" }] })
//cart.save()
module.exports = app;