-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
57 lines (50 loc) · 1.45 KB
/
Copy pathserver.js
File metadata and controls
57 lines (50 loc) · 1.45 KB
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
const http = require("http");
const express = require("express");
const app = express();
const imagens_route = require("./routes/imagens");
const getAllCategory = require("./routes/category");
const morgan = require("morgan");
const mongoose = require("mongoose");
require("dotenv").config();
const username = process.env.MONGO_USERNAME;
const password = process.env.MONGO_PASSWORD;
const url = process.env.MONGO_URL;
const auth = `mongodb+srv://${username}:${password}${url}`;
const port = process.env.PORT ? process.env.PORT : 3000;
app.use(morgan("dev"));
//app.use(express.urlencoded({ extends: true }));
app.use(express.json());
app.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Header",
"Origin, X-Requested-With, Content-Type, Accept, Authorization"
);
if (req.method === "OPTIONS") {
res.header("Access-Control-Allow-Methods", "PUT, POST, PATCH, DELETE, GET");
return res.status(200).send({});
}
next();
});
app.use("/pics", imagens_route);
app.use("/category", getAllCategory);
app.use((req, res, next) => {
const error = new Error("Não encontrado");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.send({
error: error.message,
});
});
mongoose
.connect(auth)
.then(() => {
console.log(`listening on ${port}`);
app.listen(3000);
})
.catch((err) => {
console.log(err);
});