Skip to content

Commit 77cef6f

Browse files
committed
Revert the creation of a separate app setup
1 parent 9f62308 commit 77cef6f

File tree

8 files changed

+57
-71
lines changed

8 files changed

+57
-71
lines changed

app.js

Lines changed: 41 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,61 +12,55 @@ const catalogRouter = require("./routes/catalog"); // Import routes for "catalog
1212
const compression = require("compression");
1313
const helmet = require("helmet");
1414

15-
// Create an Express application
16-
function createApp() {
17-
const app = express();
15+
const app = express();
1816

19-
// Set up rate limiter: maximum of twenty requests per minute
20-
const limiter = RateLimit({
21-
windowMs: 1 * 40 * 1000, // 10 seconds
22-
max: 40,
23-
});
24-
// Apply rate limiter to all requests
25-
app.use(limiter);
17+
// Set up rate limiter: maximum of twenty requests per minute
18+
const limiter = RateLimit({
19+
windowMs: 1 * 60 * 1000, // 10 seconds
20+
max: 60,
21+
});
22+
// Apply rate limiter to all requests
23+
app.use(limiter);
2624

27-
// view engine setup
28-
app.set("views", path.join(__dirname, "views"));
29-
app.set("view engine", "pug");
25+
// view engine setup
26+
app.set("views", path.join(__dirname, "views"));
27+
app.set("view engine", "pug");
3028

31-
app.use(logger("dev"));
32-
app.use(express.json());
33-
app.use(express.urlencoded({ extended: false }));
34-
app.use(cookieParser());
29+
app.use(logger("dev"));
30+
app.use(express.json());
31+
app.use(express.urlencoded({ extended: false }));
32+
app.use(cookieParser());
3533

36-
app.use(
37-
helmet.contentSecurityPolicy({
38-
directives: {
39-
"script-src": ["'self'", "code.jquery.com", "cdn.jsdelivr.net"],
40-
},
41-
})
42-
);
34+
app.use(
35+
helmet.contentSecurityPolicy({
36+
directives: {
37+
"script-src": ["'self'", "code.jquery.com", "cdn.jsdelivr.net"],
38+
},
39+
})
40+
);
4341

44-
app.use(compression()); // Compress all routes
42+
app.use(compression()); // Compress all routes
4543

46-
app.use(express.static(path.join(__dirname, "public")));
44+
app.use(express.static(path.join(__dirname, "public")));
4745

48-
app.use("/", indexRouter);
49-
app.use("/users", usersRouter);
50-
app.use("/catalog", catalogRouter); // Add catalog routes to middleware chain.
46+
app.use("/", indexRouter);
47+
app.use("/users", usersRouter);
48+
app.use("/catalog", catalogRouter); // Add catalog routes to middleware chain.
5149

52-
// catch 404 and forward to error handler
53-
app.use(function (req, res, next) {
54-
next(createError(404));
55-
});
50+
// catch 404 and forward to error handler
51+
app.use(function (req, res, next) {
52+
next(createError(404));
53+
});
5654

57-
// error handler
58-
app.use(function (err, req, res, next) {
59-
// set locals, only providing error in development
60-
res.locals.message = err.message;
61-
res.locals.error = req.app.get("env") === "development" ? err : {};
55+
// error handler
56+
app.use(function (err, req, res, next) {
57+
// set locals, only providing error in development
58+
res.locals.message = err.message;
59+
res.locals.error = req.app.get("env") === "development" ? err : {};
6260

63-
// render the error page
64-
res.status(err.status || 500);
65-
res.render("error");
66-
});
61+
// render the error page
62+
res.status(err.status || 500);
63+
res.render("error");
64+
});
6765

68-
return app;
69-
}
70-
71-
//module.exports = app;
72-
module.exports = { createApp };
66+
module.exports = app;

bin/www

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44
* Module dependencies.
55
*/
66

7+
var app = require("../app");
78
var debug = require("debug")("express-locallibrary-tutorial:server");
89
var http = require("http");
910

10-
const { createApp } = require("../app");
11-
const app = createApp();
12-
1311
/**
1412
* Get port from environment and store in Express.
1513
*/

test/routes/author.test.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
const request = require("supertest"); // For testing HTTP endpoints
44
const { expect } = require("chai"); // For assertions (use 'expect' style)
55

6-
// Import the Express app and create it.
7-
const { createApp } = require("../../app");
8-
const app = createApp();
6+
// Import the Express app
7+
const app = require("../../app");
98

109
// Set up test database
1110
const testDB = require("../utils/test_db");
@@ -34,9 +33,12 @@ describe("Author Routes", () => {
3433
describe("Author List Routes", () => {
3534
describe("GET /catalog/authors", () => {
3635
it("should return 200 and contain 'Author List' or similar text", async () => {
36+
console.log("OK at this point?");
3737
const res = await request(app).get("/catalog/authors").expect(200);
38+
console.log("2OK at this point?");
3839
// Adjust this check to your actual HTML output
3940
expect(res.text.toLowerCase()).to.include("author list");
41+
console.log("3OK at this point?");
4042
});
4143
});
4244
});
@@ -126,7 +128,6 @@ describe("Author Routes", () => {
126128
});
127129
});
128130

129-
// Start of Author Create Route
130131
describe("Author Create Routes", () => {
131132
it("should load the author create form", async () => {
132133
const res = await request(app).get("/catalog/author/create").expect(200);
@@ -176,8 +177,6 @@ describe("Author Routes", () => {
176177
});
177178
});
178179

179-
// Author Update Routes
180-
181180
describe("Author Update Routes", () => {
182181
let author;
183182

test/routes/book.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
const request = require("supertest"); // For testing HTTP endpoints
44
const { expect } = require("chai"); // For assertions (use 'expect' style)
55

6-
// Import the Express app and create it.
7-
const { createApp } = require("../../app");
8-
const app = createApp();
6+
// Import the Express app
7+
const app = require("../../app");
98

109
// Set up test database
1110
const testDB = require("../utils/test_db");

test/routes/bookinstance.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
const request = require("supertest"); // For testing HTTP endpoints
44
const { expect } = require("chai"); // For assertions (use 'expect' style)
55

6-
// Import the Express app and create it.
7-
const { createApp } = require("../../app");
8-
const app = createApp();
6+
// Import the Express app
7+
const app = require("../../app");
98

109
// Set up test database
1110
const testDB = require("../utils/test_db");

test/routes/catalog.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
const request = require("supertest"); // For testing HTTP endpoints
44
const { expect } = require("chai"); // For assertions (use 'expect' style)
55

6-
// Import the Express app and create it.
7-
const { createApp } = require("../../app");
8-
const app = createApp();
6+
// Import the Express app
7+
const app = require("../../app");
98

109
// Set up test database
1110
const testDB = require("../utils/test_db");

test/routes/genre.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
const request = require("supertest"); // For testing HTTP endpoints
44
const { expect } = require("chai"); // For assertions (use 'expect' style)
55

6-
// Import the Express app and create it.
7-
const { createApp } = require("../../app");
8-
const app = createApp();
6+
// Import the Express app
7+
const app = require("../../app");
98

109
// Set up test database
1110
const testDB = require("../utils/test_db");

test/routes/index.test.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
const request = require("supertest"); // For testing HTTP endpoints
44
const { expect } = require("chai"); // For assertions (use 'expect' style)
55

6-
// Import the Express app and create it.
7-
const { createApp } = require("../../app");
8-
const app = createApp();
6+
// Import the Express app
7+
const app = require("../../app");
98

109
describe("Index Routes", () => {
1110
// Block for the test suite for the root route '/'

0 commit comments

Comments
 (0)