-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
64 lines (54 loc) · 1.48 KB
/
index.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
const express = require('express');
const morgan = require('morgan');
const cors = require('cors');
const path = require('path');
const mongoose = require('mongoose');
const history = require('connect-history-api-fallback');
const routerApi = require('./routes');
let mockUser = require('./mocking/userslists.js');
require('dotenv').config();
const app = express();
// Or using promises
/* mongoose.connect(process.env.URI, process.env.OPTIONS).then(
ready to use. The `mongoose.connect()` promise resolves to mongoose instance.
() => {
console.log('Conectado a DB');
},
handle initial connection error
(err) => {
console.log(err);
},
);
*/
mongoose.connect(process.env.URI).then(
() => { console.log('conectado a la DB');}
).catch(
(err) => { console.log(err);}
)
if (process.env.NODE_ENV === 'production') {
// Definir un directorio estático
app.use(express.static(`${__dirname}/site/`));
app.get('*', (req, res) => {
res.sendFile(`${__dirname}/site/index.html`);
});
}
// Middleware
app.use(cors());
app.use(morgan('tiny'));
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
}),
);
app.get('/', (req, res) => {
res.send('HEllooo to my new server');
console.log(mockUser(15));
});
routerApi(app);
// Middleware para Vue.js router modo history
app.use(history());
app.use(express.static(path.join(__dirname, 'public')));
app.listen(process.env.PORT, () => {
console.log(`App ejemplo corriendo por el puerto: ${process.env.PORT}`);
});