Skip to content

Commit 56873d4

Browse files
committed
Feedback changes from PR 40 and 41
1 parent fa606c2 commit 56873d4

10 files changed

Lines changed: 40 additions & 25 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ functions are exported – everything else lives in internal modules and is inte
3232
kept private. Example:
3333

3434
```js
35-
import { app, createServer, start } from 'rerum_server'
35+
import { app, createServer, start } from 'rerum_server_nodejs'
3636

3737
// `app` is the configured Express application; you can pass it to Supertest or reuse it
3838
// inside another HTTP stack.

__tests__/public_api.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,6 @@ describe('public API entry point', () => {
1818
server.on('listening', () => {
1919
server.close(() => done())
2020
})
21+
server.on('error', (err) => done(err))
2122
})
2223
})

index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default app
2323
* additional listeners or configure timeouts before calling
2424
* `server.listen(...)`.
2525
*
26-
* @param {number|string} [port=process.env.PORT||3001] port to assign to
26+
* @param {number|string} [port=process.env.PORT??3001] port to assign to
2727
* the express app and eventually listen on
2828
* @returns {import('http').Server} http server instance
2929
*/
@@ -51,5 +51,18 @@ export function start(port) {
5151
server.on('listening', () => {
5252
console.log('LISTENING ON ' + p)
5353
})
54+
server.on('error', (error) => {
55+
if (error.syscall !== 'listen') throw error
56+
switch (error.code) {
57+
case 'EACCES':
58+
console.error(`Port ${p} requires elevated privileges`)
59+
process.exit(1)
60+
case 'EADDRINUSE':
61+
console.error(`Port ${p} is already in use`)
62+
process.exit(1)
63+
default:
64+
throw error
65+
}
66+
})
5467
return server
5568
}

rest.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Token: ${token}`
8686
You are Forbidden from performing this action. The request does not contain an "Authorization" header.
8787
Make sure you have registered at ${config.RERUM_PREFIX}. `
8888
}
89+
break
8990
case 404:
9091
error.message += `
9192
The requested web page or resource could not be found.`

routes/__tests__/create.test.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import express from "express"
33
import request from "supertest"
44
import { db } from '../../database/client.js'
55
import controller from '../../db-controller.js'
6+
import config from '../../config/index.js'
67

7-
const rerum_uri = `${process.env.RERUM_ID_PREFIX}123456`
8+
const rerum_uri = `${config.RERUM_ID_PREFIX}123456`
89

910
// Here is the auth mock so we get a req.user and the controller can function without a NPE.
1011
const addAuth = (req, res, next) => {
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
import { jest } from "@jest/globals"
2-
import dotenv from "dotenv"
32
import controller from '../../db-controller.js'
3+
import config from '../../config/index.js'
44

55
it("Functional '@id-id' negotiation on objects returned.", async () => {
66
let negotiate = {
77
"@context": "http://iiif.io/api/presentation/3/context.json",
88
"_id": "example",
9-
"@id": `${process.env.RERUM_ID_PREFIX}example`,
9+
"@id": `${config.RERUM_ID_PREFIX}example`,
1010
"test": "item"
1111
}
1212
negotiate = controller.idNegotiation(negotiate)
1313
expect(negotiate._id).toBeUndefined()
1414
expect(negotiate["@id"]).toBeUndefined()
15-
expect(negotiate.id).toBe(`${process.env.RERUM_ID_PREFIX}example`)
15+
expect(negotiate.id).toBe(`${config.RERUM_ID_PREFIX}example`)
1616
expect(negotiate.test).toBe("item")
1717

1818
let nonegotiate = {
1919
"@context":"http://example.org/context.json",
2020
"_id": "example",
21-
"@id": `${process.env.RERUM_ID_PREFIX}example`,
21+
"@id": `${config.RERUM_ID_PREFIX}example`,
2222
"id": "test_example",
2323
"test":"item"
2424
}
2525
nonegotiate = controller.idNegotiation(nonegotiate)
2626
expect(nonegotiate._id).toBeUndefined()
27-
expect(nonegotiate["@id"]).toBe(`${process.env.RERUM_ID_PREFIX}example`)
27+
expect(nonegotiate["@id"]).toBe(`${config.RERUM_ID_PREFIX}example`)
2828
expect(nonegotiate.id).toBe("test_example")
2929
expect(nonegotiate.test).toBe("item")
3030
})

routes/__tests__/patch.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { jest } from "@jest/globals"
2-
import dotenv from "dotenv"
3-
dotenv.config()
2+
// dotenv.config() is no longer needed; config module handles environment loading
43
// Only real way to test an express route is to mount it and call it so that we can use the req, res, next.
54
import express from "express"
65
import request from "supertest"
76
import controller from '../../db-controller.js'
7+
import config from '../../config/index.js'
88

99
// Here is the auth mock so we get a req.user and the controller can function without a NPE.
1010
const addAuth = (req, res, next) => {
@@ -23,7 +23,7 @@ const unique = new Date(Date.now()).toISOString().replace("Z", "")
2323
it("'/patch' route functions", async () => {
2424
const response = await request(routeTester)
2525
.patch('/patch')
26-
.send({"@id":`${process.env.RERUM_ID_PREFIX}11111`, "RERUM Update Test":unique})
26+
.send({"@id":`${config.RERUM_ID_PREFIX}11111`, "RERUM Update Test":unique})
2727
.set("Content-Type", "application/json")
2828
.then(resp => resp)
2929
.catch(err => err)

routes/__tests__/set.test.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { jest } from "@jest/globals"
2-
import dotenv from "dotenv"
3-
dotenv.config()
4-
5-
// Only real way to test an express route is to mount it and call it so that we can use the req, res, next.
2+
// dotenv.config() is no longer needed; config module handles environment loading
63
import express from "express"
74
import request from "supertest"
85
import controller from '../../db-controller.js'
6+
import config from '../../config/index.js'
7+
8+
// Only real way to test an express route is to mount it and call it so that we can use the req, res, next.
99

1010
// Here is the auth mock so we get a req.user and the controller can function without a NPE.
1111
const addAuth = (req, res, next) => {
@@ -24,12 +24,12 @@ const unique = new Date(Date.now()).toISOString().replace("Z", "")
2424
it("'/set' route functions", async () => {
2525
const response = await request(routeTester)
2626
.patch("/set")
27-
.send({"@id":`${process.env.RERUM_ID_PREFIX}11111`, "test_set":unique})
27+
.send({"@id":`${config.RERUM_ID_PREFIX}11111`, "test_set":unique})
2828
.set('Content-Type', 'application/json; charset=utf-8')
2929
.then(resp => resp)
3030
.catch(err => err)
3131
expect(response.header.location).toBe(response.body["@id"])
32-
expect(response.headers["location"]).not.toBe(`${process.env.RERUM_ID_PREFIX}11111`)
32+
expect(response.headers["location"]).not.toBe(`${config.RERUM_ID_PREFIX}11111`)
3333
expect(response.statusCode).toBe(200)
3434
expect(response.body._id).toBeUndefined()
3535
expect(response.body["test_set"]).toBe(unique)

routes/__tests__/unset.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { jest } from "@jest/globals"
2-
import dotenv from "dotenv"
3-
dotenv.config()
2+
// dotenv.config() is no longer needed; config module handles environment loading
43

54
// Only real way to test an express route is to mount it and call it so that we can use the req, res, next.
65
import express from "express"
76
import request from "supertest"
87
import controller from '../../db-controller.js'
8+
import config from '../../config/index.js'
99

1010
// Here is the auth mock so we get a req.user so controller.create can function without a NPE.
1111
const addAuth = (req, res, next) => {
@@ -23,7 +23,7 @@ routeTester.use("/unset", [addAuth, controller.patchUnset])
2323
it("'/unset' route functions", async () => {
2424
const response = await request(routeTester)
2525
.patch("/unset")
26-
.send({"@id":`${process.env.RERUM_ID_PREFIX}11111`, "test_obj":null})
26+
.send({"@id":`${config.RERUM_ID_PREFIX}11111`, "test_obj":null})
2727
.set('Content-Type', 'application/json; charset=utf-8')
2828
.then(resp => resp)
2929
.catch(err => err)

routes/__tests__/update.test.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { jest } from "@jest/globals"
2-
import dotenv from "dotenv"
3-
dotenv.config()
4-
// Only real way to test an express route is to mount it and call it so that we can use the req, res, next.
52
import express from "express"
63
import request from "supertest"
74
import controller from '../../db-controller.js'
5+
import config from '../../config/index.js'
6+
// Only real way to test an express route is to mount it and call it so that we can use the req, res, next.
87

98
// Here is the auth mock so we get a req.user so controller.create can function without a NPE.
109
const addAuth = (req, res, next) => {
@@ -24,12 +23,12 @@ it("'/update' route functions", async () => {
2423

2524
const response = await request(routeTester)
2625
.put('/update')
27-
.send({"@id":`${process.env.RERUM_ID_PREFIX}11111`, "RERUM Update Test":unique})
26+
.send({"@id":`${config.RERUM_ID_PREFIX}11111`, "RERUM Update Test":unique})
2827
.set("Content-Type", "application/json")
2928
.then(resp => resp)
3029
.catch(err => err)
3130
expect(response.header.location).toBe(response.body["@id"])
32-
expect(response.headers["location"]).not.toBe(`${process.env.RERUM_ID_PREFIX}11111`)
31+
expect(response.headers["location"]).not.toBe(`${config.RERUM_ID_PREFIX}11111`)
3332
expect(response.statusCode).toBe(200)
3433
expect(response.body._id).toBeUndefined()
3534
expect(response.body["RERUM Update Test"]).toBe(unique)

0 commit comments

Comments
 (0)