Skip to content

Commit 1024b99

Browse files
fix: support Express v4 and v5
1 parent cac236f commit 1024b99

File tree

2 files changed

+58
-21
lines changed

2 files changed

+58
-21
lines changed

API/api.js

Lines changed: 57 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,14 @@ module.exports = {
115115

116116
this.expressRouter = express.Router();
117117

118+
/*
119+
* MagicMirror² switches to Express 5 with v2.32.0 - to keep compatibility with older versions we need
120+
* to check for the Express version. Since Express 5 dropt the .del method, we can use that to check.
121+
* If the method is not available, we are using Express 4.x and need to use the old syntax.
122+
* This is a temporary solution and will be removed in the future.
123+
*/
124+
const expressVersionLessThan5 = express.application.del ? true : false;
125+
118126
// Route for testing the api at http://mirror:8080/api/test
119127
this.expressRouter.route(["/test", "/"]). // Test without apiKey
120128
get((req, res) => {
@@ -168,21 +176,35 @@ module.exports = {
168176
self.answerGet({data: r}, res);
169177
});
170178

171-
this.expressRouter.route([
172-
"/refresh{/:delayed}",
173-
"/shutdown{/:delayed}",
174-
"/reboot{/:delayed}",
175-
"/restart{/:delayed}",
176-
"/save",
177-
"/minimize",
178-
"/togglefullscreen",
179-
"/devtools"
180-
]).get((req, res) => {
181-
if (!this.apiKey && this.secureEndpoints) { return res.status(403).json({success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message"}); }
182-
const r = req.path.split("/")[1].toUpperCase();
183-
console.log(req.path);
184-
self.executeQuery(this.checkDelay({action: r}, req), res);
185-
});
179+
let route = expressVersionLessThan5
180+
? [
181+
"/refresh/:delayed?",
182+
"/shutdown/:delayed?",
183+
"/reboot/:delayed?",
184+
"/restart/:delayed?",
185+
"/save",
186+
"/minimize",
187+
"/togglefullscreen",
188+
"/devtools"
189+
]
190+
: [
191+
"/refresh{/:delayed}",
192+
"/shutdown{/:delayed}",
193+
"/reboot{/:delayed}",
194+
"/restart{/:delayed}",
195+
"/save",
196+
"/minimize",
197+
"/togglefullscreen",
198+
"/devtools"
199+
];
200+
201+
this.expressRouter.route(route).
202+
get((req, res) => {
203+
if (!this.apiKey && this.secureEndpoints) { return res.status(403).json({success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message"}); }
204+
const r = req.path.split("/")[1].toUpperCase();
205+
console.log(req.path);
206+
self.executeQuery(this.checkDelay({action: r}, req), res);
207+
});
186208

187209
this.expressRouter.route("/classes/:value").
188210
get((req, res) => {
@@ -201,7 +223,10 @@ module.exports = {
201223
self.executeQuery({action: "COMMAND", command: req.params.value}, res);
202224
});
203225

204-
this.expressRouter.route("/userpresence{/:value}").
226+
route = expressVersionLessThan5
227+
? "/userpresence/:value?"
228+
: "/userpresence{/:value}";
229+
this.expressRouter.route(route).
205230
get((req, res) => {
206231
if (req.params.value) {
207232
if (req.params.value === "true" || req.params.value === "false") {
@@ -214,7 +239,10 @@ module.exports = {
214239
}
215240
});
216241

217-
this.expressRouter.route("/update{/:moduleName}").
242+
route = expressVersionLessThan5
243+
? "/update/:moduleName?"
244+
: "/update{/:moduleName}";
245+
this.expressRouter.route(route).
218246
get((req, res) => {
219247
if (!this.apiKey && this.secureEndpoints) { return res.status(403).json({success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message"}); }
220248
if (!req.params.moduleName) { return self.answerGet({data: "mmUpdateAvailable"}, res); }
@@ -253,7 +281,10 @@ module.exports = {
253281
});
254282
// edit config
255283

256-
this.expressRouter.route("/notification/:notification{/:p}{/:delayed}").
284+
route = expressVersionLessThan5
285+
? "/notification/:notification/:p?/:delayed?"
286+
: "/notification/:notification{/:p}{/:delayed}";
287+
this.expressRouter.route(route).
257288
get((req, res) => {
258289
if (!this.apiKey && this.secureEndpoints) { return res.status(403).json({success: false, message: "Forbidden: API Key Not Provided in Config! Use secureEndpoints to bypass this message"}); }
259290
this.answerNotifyApi(req, res);
@@ -267,7 +298,10 @@ module.exports = {
267298
this.answerNotifyApi(req, res);
268299
});
269300

270-
this.expressRouter.route("/module{/:moduleName}{/:action}{/:delayed}").
301+
route = expressVersionLessThan5
302+
? "/module/:moduleName?/:action?/:delayed?"
303+
: "/module{/:moduleName}{/:action}{/:delayed}";
304+
this.expressRouter.route(route).
271305
get((req, res) => {
272306
this.answerModuleApi(req, res);
273307
}).
@@ -279,7 +313,10 @@ module.exports = {
279313
this.answerModuleApi(req, res);
280314
});
281315

282-
this.expressRouter.route("/monitor{/:action}{/:delayed}").
316+
route = expressVersionLessThan5
317+
? "/monitor/:action?/:delayed?"
318+
: "/monitor{/:action}{/:delayed}";
319+
this.expressRouter.route(route).
283320
get((req, res) => {
284321
if (!req.params.action) { req.params.action = "STATUS"; }
285322
const actionName = req.params.action.toUpperCase();

eslint.config.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export default defineConfig([
3636
"capitalized-comments": "off",
3737
"consistent-this": "off",
3838
"line-comment-position": "off",
39-
"max-lines-per-function": ["warn", 220],
39+
"max-lines-per-function": ["warn", 250],
4040
"max-statements": ["warn", 105],
4141
"multiline-comment-style": "off",
4242
"no-await-in-loop": "off",

0 commit comments

Comments
 (0)