-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
61 lines (47 loc) · 1.75 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
let express = require("express"),
mongoose = require("mongoose"),
bodyParser = require("body-parser");
let db = mongoose.connect("mongodb://localhost/movieAPI");
let Movie = require("./models/movieModel");
let app = express();
let port = 41502;
app.options(function(req, res, next) {
res.header("Access-Control-Allow-Methods", "GET,POST,OPTIONS");
res.header(
"Access-Control-Allow-Header",
"Content-Type, Accept, Authorization, Content-Length, X-Request-Width"
);
res.send(200);
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "origin, content-type, accept");
if (req.accepts("json" || "xml" || "x-www-form-urlencoded")) {
next();
} else {
res.sendStatus(406);
}
});
movieRouter = require("./Routes/movieRoutes")(Movie, app);
app.use("/api/movies", movieRouter);
app.post('/api/movies', function (req, res) {
console.log('ja!!');
res.sendStatus(201)
return
let movies = new Movie(req.body);
movies._link.self.href = 'http://' + req.headers.host + '/api/movies/' + movie._id;
movies._link.collection.href = 'http://' + req.headers.host + '/api/movies/';
console.log(req.body);
if (!req.body.title || !req.body.author || !req.body.genre) {
return res.status(404).send('Geen lege velden !')
}
movie.save();
})
app.get("/", function(req, res) {
res.send("Welcome to my API!");
});
app.listen(port, function() {
console.log("Running on port: " + port);
});