-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (50 loc) · 1.72 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
54
55
56
57
58
59
60
61
62
63
require("babel/register");
// use babel-node to run this file
import express from "express";
import bodyParser from "body-parser";
import mongoose from "mongoose";
import logarithmic from "logarithmic";
import Image from "./models/image";
import wordmap from "./lib/wordmap";
import areSimilar from "./lib/aresimilar";
mongoose.connect("localhost");
mongoose.connection.on("error", () => {
logarithmic.error("Could not connect to Mongoose");
});
const app = express();
app.use(express.static(`${__dirname}/public`));
app.use(bodyParser());
mongoose.connection.on("open", () => {
logarithmic.ok("Connected to Mongoose correctly");
Image.find({}, (err, images) => {
console.log(`There are ${images.length} images stored in the database`);
});
app.get("/api/related/", (request, response) => {
console.log(request);
const keywords = request.query.keywords.split(",").map((word) => word.trim());
Image.find({}, (error, images) => {
const isNotInArray = (array) => {
return (element) => array.indexOf(element) === -1;
};
let relatedImages = [];
for (let image of images) {
if (keywords.filter(isNotInArray(image.keywords)).length === 0) { // if all keywords are in the image
relatedImages.push(image);
}
}
response.send(relatedImages);
});
});
});
app.get("/", (request, response) => {
response.sendFile(__dirname + "/index.html");
});
app.get('/stylesheets/styles.css', (request, response) => {
response.sendFile(__dirname + "/stylesheets/styles.css");
});
app.get('/index-pics/:n', (request, response) => {
response.sendFile(__dirname + "/index-pics/" + request.params.n);
});
app.listen(80, () => {
logarithmic.ok("Server has started up");
});