-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
114 lines (96 loc) · 3.53 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const express = require("express");
const cors = require("cors");
const jwt = require("jsonwebtoken");
var path = require("path");
const mongoose = require("mongoose");
fs = require("fs");
const sharp = require("sharp");
const authRoutes = require("./routes/Auth");
const vagaRoutes = require("./routes/Vaga");
const userRoutes = require("./routes/User");
const empresaRoutes = require("./routes/Empresa");
const multer = require("multer");
const app = express();
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const user = "ricardo";
const password = "Azd202020";
const database = "dbOne";
const server = `mongodb+srv://${user}:${password}@cluster0.94tuo.mongodb.net/${database}?retryWrites=true&w=majority`;
const config = { useNewUrlParser: true, useUnifiedTopology: true };
mongoose.connect(server, config).then(() => {
console.log("Database connection successfully!");
});
const port = 5556;
const version = 0;
app.use("/images", express.static(path.join(__dirname, "assets")));
app.use(`/auth`, authRoutes);
app.use(`/core`, vagaRoutes);
app.use(`/user`, userRoutes);
app.use(`/empresa`, empresaRoutes);
const multerFilter = (req, file, cb) => {
if (file.mimetype.startsWith("image")) {
cb(null, true);
} else {
cb("Please upload only images.", false);
}
};
const upload = multer({
fileFilter: multerFilter,
});
app.post("/image", upload.array(), function (req, res) {
const { key, base64 } = req.body;
let imgBuffer = Buffer.from(base64, "base64");
sharp(imgBuffer)
.resize(400, 400)
.toBuffer()
.then((data) => {
console.log("Imagem adicionada com sucesso");
fs.writeFile(
__dirname + `/upload/${key}.jpeg`,
data,
function (err, data) {
if (err) {
console.log("err", err);
}
var stats = fs.statSync(__dirname + `/upload/${key}.jpeg`);
var fileSizeInBytes = stats.size;
var fileSizeInMegabytes = fileSizeInBytes / (1024 * 1024);
console.log("tamanho da imagem", fileSizeInMegabytes + "Mb");
}
);
})
.catch((error) => {
console.log("error", error);
});
// console.log('writing file...', base64Data);
// fs.writeFile(__dirname + "/upload/out.png", base64Data, 'base64', function(err) {
// if (err) console.log(err);
// fs.readFile(__dirname + "/upload/out.png", function(err, data) {
// if (err) throw err;
// console.log('reading file...', data.toString('base64'));
// res.send(data);
// });
// });
});
// [ POST ] vernagro.com.br/api/v0/auth/login
// [ POST ] vernagro.com.com/api/v0/auth/produtor
// [ POST ] vernagro.com.com/api/v0/auth/consultor
// [ POST ] vernagro.com.com/api/v0/auth/diretor
// [ POST ] vernagro.com.com/api/v0/auth/comercial
// [ POST ] vernagro.com.com/api/v0/core/empresa
// [ GET ] vernagro.com.com/api/v0/core/empresa
// [ DELETE ] vernagro.com.com/api/v0/core/empresa
// [ GET ] vernagro.com.com/api/v0/core/empresas
// [ POST ] vernagro.com.com/api/v0/core/filial
// [ GET ] vernagro.com.com/api/v0/core/filial
// [ DELETE ] vernagro.com.com/api/v0/core/filial
// [ GET ] vernagro.com.com/api/v0/core/filiais
// [ POST ] vernagro.com.com/api/v0/core/pde
// [ GET ] vernagro.com.com/api/v0/core/pde
// [ DELETE ] vernagro.com.com/api/v0/core/pde
// [ GET ] vernagro.com.com/api/v0/core/pdes
app.listen(port, () => {
console.log("Servidor rodando na porta", port);
});