Skip to content

Commit

Permalink
expanded the express-example
Browse files Browse the repository at this point in the history
  • Loading branch information
miquelbeltran committed May 14, 2024
1 parent 5fee12a commit fb7c68f
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 20 deletions.
11 changes: 7 additions & 4 deletions examples/express-sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ in the subdirectory where you found this README.md file.

## Interesting files to look

- `logger.js`
- Setup of Raygun (lines 9-14)
- `app.js`
- Setup of Raygun (lines 9-12)
- Sets the user (lines 27-29)
- Attaches Raygun to Express (line 60)
- Sets the user (lines 17-19)
- Attaches Raygun Breadcrumb middleware to Express (line 26)
- Attaches Raygun to Express (line 53)
- `routes/index.js`
- Tries to use a fake object, which bounces up to the Express handler (lines 11-15)
- `/send` endpoint: Sends a custom error to Raygun (lines 11-34)
- `/error` endpoint: Tries to use a fake object, which bounces up to the Express handler (lines 36-49)
14 changes: 1 addition & 13 deletions examples/express-sample/app.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
var config = require("config");

if (config.Raygun.Key === "YOUR_API_KEY") {
console.error(
"[Raygun4Node-Express-Sample] You need to set your Raygun API key in the config file",
);
process.exit(1);
}

// Setup Raygun
var raygun = require("raygun");
var raygunClient = new raygun.Client().init({
apiKey: config.Raygun.Key,
});

var express = require("express");
var path = require("path");
var logger = require("morgan");
var cookieParser = require("cookie-parser");
var bodyParser = require("body-parser");
var sassMiddleware = require("node-sass-middleware");
var raygunClient = require("./logger");

var routes = require("./routes/index");
var users = require("./routes/users");
Expand Down
16 changes: 16 additions & 0 deletions examples/express-sample/logger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var config = require("config");

if (config.Raygun.Key === "YOUR_API_KEY") {
console.error(
"[Raygun4Node-Express-Sample] You need to set your Raygun API key in the config file",
);
process.exit(1);
}

// Setup Raygun
var raygun = require("raygun");
var raygunClient = new raygun.Client().init({
apiKey: config.Raygun.Key,
});

module.exports = raygunClient;
40 changes: 37 additions & 3 deletions examples/express-sample/routes/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,48 @@
var express = require("express");
var router = express.Router();
const express = require("express");
const router = express.Router();
const raygunClient = require("../logger");

/* GET home page. */
router.get("/", function (req, res, next) {
res.render("index", {
title: "Express",
title: "Raygun Express Example",
});
});

router.get("/send", function (req, res, next) {

raygunClient.addBreadcrumb({
level: "debug",
category: "Example",
message: "Breadcrumb in /send endpoint",
customData: {
"custom-data": "data",
}
});

raygunClient.send("Error in /send endpoint").then((message) => {
res.render("send", {
title: "Sent custom error to Raygun",
body: `Raygun status code: ${message.statusCode}`,
});
}).catch((error) => {
res.render("send", {
title: "Failed to send custom error to Raygun",
body: error.toString(),
});
})
});

router.get("/error", function (req, res, next) {
raygunClient.addBreadcrumb({
level: "debug",
category: "Example",
message: "Breadcrumb in /error endpoint",
customData: {
"custom-data": "data",
}
});

// Call an object that doesn't exist to send an error to Raygun
fakeObject.FakeMethod();
res.send(500);
Expand Down
3 changes: 3 additions & 0 deletions examples/express-sample/views/index.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<h1><%= title %></h1>
<p>Welcome to <%= title %></p>

<div>
<a href="/send">Send a custom error</a>
</div>
<div>
<a href="/error">Throw an error</a>
</div>
Expand Down
18 changes: 18 additions & 0 deletions examples/express-sample/views/send.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!DOCTYPE html>
<html>
<head>
<title><%= title %></title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>
<body>
<h1><%= title %></h1>
<p><%= title %></p>
<div>
<%= body %>
</div>

<div>
<a href="/">Back to index</a>
</div>
</body>
</html>

0 comments on commit fb7c68f

Please sign in to comment.