-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·64 lines (51 loc) · 1.91 KB
/
app.js
File metadata and controls
executable file
·64 lines (51 loc) · 1.91 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
// ------------------------------------------------------------------------
// app.js - This is a generic Node application provide by the Express cli,
// routing to both the default and "content" functional locations.
// NOTE: This does use EJS-based templating; if you choose to use
// something else (like Angular or React), you will need to change
// the view engine.
// ------------------------------------------------------------------------
const express = require("express");
const path = require("path");
// These are the additional packages we need for this application
const maxAPI = require("max-api");
const cors = require("cors");
// set up the express app
const app = express();
// this is the middleware that allows CORS
app.use(cors());
app.use(express.json());
// handle sequencer points
app.post("/sequencer", (req, res) => {
if (req.body.action === "add") {
maxAPI.outlet("add", req.body.x, req.body.y, req.body.color);
res.status(200).send("Added point");
} else if (req.body.action === "remove") {
maxAPI.outlet("remove", req.body.x, req.body.y, req.body.color);
res.status(200).send("Removed point");
} else {
res.status(400).send("Invalid action");
}
});
// serve index for all other paths
app.use(express.static(path.resolve(__dirname, "client/dist")));
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client/dist", "index.html"));
});
// catch 404 and forward to error handler
app.use((req, res, next) => {
var err = new Error("Not Found");
err.status = 404;
next(err);
});
// error handler
app.use((err, req, res) => {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get("env") === "development" ? err : {};
// render the error page
res.status(err.status || 500);
res.render("error");
});
maxAPI.post("Started file handler app");
module.exports = app;