-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
56 lines (42 loc) · 1.49 KB
/
index.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
const restify = require('restify');
const fs = require('fs');
const config = require('./config');
let server = restify.createServer({ name: config.NAME })
// Get list of controllers
let controllers = {};
let controllers_path = process.cwd() + '/app/controller';
fs.readdirSync(controllers_path).forEach(function (file) {
if (file.indexOf('.js') != -1) {
controllers[file.split('.')[0]] = require(controllers_path + '/' + file);
}
});
// Middleware
server.use(restify.plugins.acceptParser(server.acceptable));
server.use(restify.plugins.queryParser());
server.use(restify.plugins.bodyParser({ mapParams: true, overrideParams: false }));
server.use(
function crossOrigin(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
return next();
}
);
// Backend API endpoints
server.get("/health", function (req, res, next) {
res.json(200, "beep boop");
next()
});
server.get("/all", controllers.search.getAll);
server.get("/search", controllers.search.search);
server.post("/add", controllers.data.addTweet);
server.post("/delete", controllers.data.deleteData);
server.post("/update", controllers.data.updateFav);
// Static content
server.get("/*", restify.plugins.serveStatic({
directory: process.cwd() + "/view",
default: 'index.html'
}));
// Gets the server up and listening
server.listen(config.PORT, () => {
console.log("%s listening at %s", server.name, server.url);
})