-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
52 lines (47 loc) · 2.29 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
/*
* Copyright 2023 Jack Henry & Associates, Inc.®
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const express = require('express');
const app = express();
const fetch = require('node-fetch');
// Define the port the Express application will run on
const app_port = 8080;
// Set EJS as the application's view engine for rendering dynamic HTML pages
app.set('view engine', 'ejs');
// Defines a route (/dynamic) which responds to HTTP GET requests. This route used for dynamic data retrieval.
app.get('/dynamic', async (req, res) => {
try {
// Asynchronously send a request to the external API "HTTP Cats" - this API humorously provides HTTP status code images
const response = await fetch('https://http.cat/status/200');
// Fetch the status code from the response
const response_code = await response.status;
// If the response status code is 200 (OK), send data to the UI template with a success message
if (response_code == 200) {
res.render('pages/dynamic', { response_code: response_code, message: "External data fetch was successful." });
} else {
// If the response status code is not 200, send data to the UI template with a failure message
res.render('pages/dynamic', { response_code: response_code, message: "External data fetch was unsuccessful. This is an error state for your plugin." });
}
} catch (error) {
// Log any errors to the console and send a 500 (Internal Server Error) status code to the client
console.error(error);
res.status(500).send('Error occurred while fetching data from the API.');
}
});
// Start the server listening on the defined port
app.listen(app_port, () => {
// Log message to console when server is running
console.log(`App running at http://localhost:${app_port}`);
});