-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (56 loc) · 1.92 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
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var mongoose = require('mongoose'); //database
mongoose.Promise = require('bluebird');
var app = express();
//database connection
var uri = 'mongodb://127.0.0.1:27017/orcamento'
mongoose.connect(uri, { useMongoClient: true });
// CONNECTION EVENTS
// When successfully connected
mongoose.connection.on('connected', function() {
console.log('Mongoose default connection open to ' + uri);
});
// If the connection throws an error
mongoose.connection.on('error', function(err) {
console.log('Mongoose default connection error: ' + err);
});
// When the connection is disconnected
mongoose.connection.on('disconnected', function() {
console.log('Mongoose default connection disconnected');
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// carregamento de rotas
var main = require('./routes/main'); // inicial
var orcamento = require('./routes/orcamento');
var produto = require('./routes/produto');
//enable cors
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
next();
});
// use routes
app.use('/', main);
app.use('/orcamento', orcamento);
app.use('/produto', produto);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
// var err = new Error('Not Found');
// err.status = 404;
// next(err);
res.send('404');
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
console.log('error');
console.log(err.message);
});
module.exports = app;