-
Notifications
You must be signed in to change notification settings - Fork 621
/
server.js
47 lines (40 loc) · 1.1 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
var express = require("express");
var app = express();
var path = require("path");
var server = require("http").createServer(app);
var fs = require("fs");
var bodyParser = require("body-parser");
app.use(bodyParser.json());
app.get("/", function(req, res) {
res.sendFile(path.join(__dirname + "/index.html"));
});
var directory;
fs.readFile("./directory.json", "utf8", function(err, data) {
directory = JSON.parse(data);
if (err) {
throw err;
}
});
app.get("/search", function(req, res) {
var results = directory.reduce(function(acc, file) {
if (file.tags.indexOf(req.query.q) !== -1) {
acc.push({
id: file.id,
title: file.title,
thumb: "/public/images/".concat(file.thumb),
price: file.price
});
}
return acc;
}, []);
res.send(results);
});
app.use("/node_modules", express.static(path.join(__dirname, "node_modules")));
app.use("/public", express.static(path.join(__dirname, "public")));
if (process.env.NODE_ENV !== "production") {
require("reload")(app);
}
var port = process.env.PORT || 3000;
server.listen(port, function () {
console.log("Listening on port ".concat(port));
});