-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
53 lines (41 loc) · 1.37 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
var express = require('express');
var server = express();
var fortune = require('fortune');
var nedbAdapter = require('fortune-nedb');
var jsonapi = require('fortune-json-api');
// Beállítjuk a JSON API serializer-t
var store = fortune({
adapter: {
type: nedbAdapter,
options: { dbPath: __dirname + '/.db' }
},
serializers: [{ type: jsonapi }]
});
// Express middleware
server.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Origin', '*');
next();
});
server.use(fortune.net.http(store));
// Csak akkor fusson a szerver, ha sikerült csatlakozni a tárolóhoz
// Hasonlóan a Waterline-hoz
store.defineType('recipe', {
name: {type: String},
type: {type: String},
base: {type: String},
recipe: {type: String},
posts: { link: 'comment', inverse: 'author', isArray: true }
});
store.defineType('comment', {
user: {type: String},
text: {type: String},
author: { link: 'recipe', inverse: 'posts' }
});
var port = process.env.PORT || 8080;
store.connect().then(function () {
server.listen(port, function () {
console.log('JSON Api server started on port ' + port);
});
});