-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
55 lines (49 loc) · 1.25 KB
/
Copy pathserver.js
File metadata and controls
55 lines (49 loc) · 1.25 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
// Create express app
const express = require("express")
const app = express()
const db = require("./database.js")
// Server port
const HTTP_PORT = 8000
// Start server
app.listen(HTTP_PORT, () => {
console.log("Server running on port %PORT%".replace("%PORT%",HTTP_PORT))
});
// Root endpoint
app.get("/", (req, res, next) => {
res.json({"message":"Ok"})
});
// Insert here other API endpoints
// Gets all markers from database.
app.get("/api/raw", (req, res, next) => {
var sql = "select * from raw"
var params = []
db.all(sql, params, (err, rows) => {
if (err) {
res.status(400).json({"error": err.message})
return
}
res.json({
"message": "success",
"data": rows
})
})
})
// Get a specific user by ID.
app.get("/api/user/:id", (req, res, next) => {
var sql = "select * from user where id = ?"
var params = [req.params.id]
db.get(sql, params, (err,row) => {
if (err) {
res.status(400).json({"error":err.message})
return
}
res.json({
"message":"success",
"data":row
})
})
})
// Default response for any other request
app.use(function(req, res){
res.status(404);
});