-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (51 loc) · 1.54 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
58
59
60
61
62
// PACOTES
const compression = require("compression");
const express = require("express");
const ejs = require("ejs");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const morgan = require("morgan");
const cors = require("cors");
// START
const app = express();
// AMBIENTE
const isProduction = process.env.NODE_ENV === "production";
const PORT = process.env.PORT || 3000;
// ARQUIVOS ESTATICOS
app.use("/public", express.static(__dirname + "/public"));
app.use("/public/images", express.static(__dirname + "/public/images"));
// SETUP MONGODB
const dbs = require("./config/database");
const dbURI = isProduction ? dbs.dbProduction : dbs.dbTest;
mongoose.connect(dbURI, { useNewUrlParser: true });
// SETUP EJS
app.set("view engine", "ejs");
// CONFIGURACOES
if(!isProduction) app.use(morgan("dev"));
app.use(cors());
app.disable('x-powered-by');
app.use(compression());
// SETUP BODY PARSER
app.use(bodyParser.urlencoded({ extended: false, limit: 1.5*1024*1024 }));
app.use(bodyParser.json({ limit: 1.5*1024*1024 }));
// MODELS
require("./models");
// ROTAS
app.use("/", require("./routes"));
// 404 - ROTA
app.use((req, res, next) => {
const err = new Error("Not Found");
err.status = 404;
next(err);
});
// ROTA - 422, 500, 401
app.use((err, req, res, next) => {
res.status(err.status || 500);
if(err.status !== 404) console.warn("Error: ", err.message, new Date());
res.json(err);
});
// ESCUTAR
app.listen(PORT, (err) => {
if(err) throw err;
console.log(`Rodando na //localhost:${PORT}`);
});