-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
57 lines (40 loc) · 1.45 KB
/
server.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
var express = require('express')
var bodyParser = require('body-parser')
const path = require("path");
require('dotenv').config()
var app = express()
var http = require('http').Server(app)
var mongoose = require('mongoose')
var fs = require('fs');
// writeFile function with filename, content and callback function
fs.writeFile('apiBaseUrl.txt', process.env.APIBASEURL, function(err) {
if (err) throw err;
console.log('API Base Url saved successfully.');
});
app.use(express.static(__dirname))
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: false }))
var frontoffice = require('./routes/frontoffice')
var backoffice = require('./routes/backoffice')
var api = require('./routes/api')
const dbUrl = process.env.DBURL
mongoose.connect(dbUrl, { useUnifiedTopology: true, useNewUrlParser: true }, (err) => {
console.log('Conectado ao MongoDB. Errors:', err)
})
var server = app.listen(process.env.PORT, () => {
console.log('O server está à escuta na porta', server.address().port)
})
//FRONTEND (REDENDERIZAÇÃO DAS PÁGINAS)
//frontoffice
app.use('/', frontoffice)
//backoffice
app.use('/administracao', backoffice)
//API BACKEND
app.use('/api', api)
//Dexar estes métodos sempre no fim do código!
app.get('/administracao/*', function(req, res) {
res.sendFile(path.join(__dirname + "/public/404backoffice.html"))
});
app.get('*', function(req, res) {
res.sendFile(path.join(__dirname + "/public/404frontoffice.html"))
});