-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (65 loc) · 1.77 KB
/
server.js
File metadata and controls
75 lines (65 loc) · 1.77 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
const express = require("express");
const fs = require("fs");
const sqlite = require("sqlite3").verbose();
const db = new sqlite.Database("./db/music.sqlite3");
const app = express();
app.set("port", process.env.PORT || 3001);
// Express only serves static assets in production
if (process.env.NODE_ENV === "production") {
app.use(express.static("client/build"));
}
const COLUMNS_ARTIST = [
"artist.name as artist_name",
"artist.picture as artist_picture",
"artist.address as artist_address"
];
const COLUMNS_SONG = [
"song.id",
"song.name",
"song.source",
"song.copyright"
];
app.get("/api/eeg", (req, res) => {
db.serialize(function() {
// WARNING: Not for production use! The following statement
// is not protected against SQL injections.
const r = db.all(
`
select value from wave order by id desc limit 10;
`
, (err, rows) =>{
if (rows){
res.json(rows);
}else{
res.json([]);
}});
});
});
app.get("/api/song", (req, res) => {
const param = req.query.q;
if (!param) {
res.json({
error: "Missing required parameter `q`"
});
return;
}
db.serialize(function() {
// WARNING: Not for production use! The following statement
// is not protected against SQL injections.
const r = db.all(
`
select ${COLUMNS_ARTIST.join(", ")}, ${COLUMNS_SONG.join(", ")} from artist inner join song
on artist.id = song.artist_id where artist.name like '%${param}%' or song.name like '%${param}%'
limit 100
`
, (err, rows) =>{
if (rows){
res.json(rows);
}else{
res.json([]);
}});
});
});
app.listen(app.get("port"), () => {
console.log(`Find the server at: http://localhost:${app.get("port")}/`); // eslint-disable-line no-console
});