-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
42 lines (36 loc) · 1.02 KB
/
Copy pathserver.js
File metadata and controls
42 lines (36 loc) · 1.02 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
const Express = require("express");
const Mongoose = require("mongoose");
const cors = require("cors");
const BodyParser = require("body-parser");
Mongoose.connect(
"mongodb+srv://cugler:1022890244@hackathon-3lkrb.mongodb.net/hackathon?retryWrites=true&w=majority"
);
var app = Express();
app.use(cors());
const UsuarioModel = Mongoose.model("person", {
nomeId: String,
resId: String
});
app.use(BodyParser.json());
app.use(BodyParser.urlencoded({ extended: true }));
app.post("/usuario", async (request, response) => {
try {
console.log(request.body);
var person = new UsuarioModel(request.body);
var result = await person.save();
response.send(result);
} catch (error) {
response.status(500).send(error);
}
});
app.get("/usuario/:id", async (request, response) => {
try {
var person = await UsuarioModel.findById(request.params.id).exec();
response.send(person);
} catch (error) {
response.status(500).send(error);
}
});
app.listen(3000, () => {
console.log("Listening at :3000...");
});