Skip to content
Snippets Groups Projects
Commit f596e81b authored by Lucendio's avatar Lucendio
Browse files

Update server dependencies; fix invocation in 'make test'

* update mongoose, jest and nodemon to address security alerts triggered
  by dependabot
* 'test' target had a bug introduced by using wrong syntax
* renamed server scripts to clarify the distinction between invocations
  in local development, CI and actual operations
parent 7d2dd163
No related branches found
No related tags found
No related merge requests found
......@@ -80,7 +80,7 @@ run-db: | $(LOG_DIR)/ $(DATA_DIR)/
run-local: export PATH := $(BIN_DIR):$(PATH)
run-local: build
cd $(MKFILE_DIR)/app/server \
&& npm start
&& npm run start:dev
.PHONY: test
......@@ -90,10 +90,11 @@ test:
cd $(MKFILE_DIR)/app/client \
&& npm run test
cd $(MKFILE_DIR)/app/server \
PORT=3002 \
MONGODB_URL=mongodb://localhost:27017/$(randomString) \
JWT_SECRET=$(randomString) \
&& npm run test
&& \
PORT=3002 \
MONGODB_URL=mongodb://localhost:27017/$(randomString) \
JWT_SECRET=$(randomString) \
npm run test
.PHONY: test-client-local
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
{
"name": "add-todo-server",
"name": "todo-app-server",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"start": "env-cmd -f ./dev.env nodemon src/index.js",
"test": "jest --ci",
"start": "node src/index.js",
"start:dev": "env-cmd -f ./dev.env nodemon src/index.js",
"test": "jest --ci --detectOpenHandles",
"test:dev": "env-cmd -f ./dev.env jest"
},
"devDependencies": {
"@shelf/jest-mongodb": "^1.1.5",
"env-cmd": "^10.1.0",
"jest": "^25.1.0",
"nodemon": "^2.0.2"
"jest": "^26.4.2",
"nodemon": "^2.0.4"
},
"dependencies": {
"bcryptjs": "^2.4.3",
......@@ -21,7 +21,7 @@
"express": "^4.17.1",
"helmet": "^3.22.0",
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.9.7",
"mongoose": "^5.10.7",
"validator": "^13.0.0"
},
"engines": {
......
const mongoose = require('mongoose');
// utilize event emitter to indicate connection retries in logs
// DOCS: https://mongoosejs.com/docs/connections.html#connection-events
const CONNECTION_EVENTS = [
'connecting', 'connected', 'disconnecting', 'disconnected',
'close', 'reconnectFailed', 'reconnected', 'error'
]
if( process.env.NODE_ENV === 'production' ){
CONNECTION_EVENTS.forEach(( eventName )=>{
return mongoose.connection.on( eventName, ()=>{
console.log( `Connection state changed to: ${ eventName }` );
});
});
}
const mongooseInstance_ = mongoose.connect(
process.env.MONGODB_URL,
{
......@@ -11,18 +27,18 @@ const mongooseInstance_ = mongoose.connect(
useUnifiedTopology: true,
heartbeatFrequencyMS: 1000 * 5, // 1 sec * 5
serverSelectionTimeoutMS: 1000 * 10 // 1 sec * 10
},
// TODO: make use of the event emitter to indicate each retry in logs & to increase
// the overall timeout to 10 min (see https://mongoosejs.com/docs/connections.html#connection-events)
function( err ){
if( typeof err !== 'undefined' && err !== null ){
console.error( new Error( `Cannot connect to database: ${ process.env.MONGODB_URL }` ) );
}else{
console.log( `Connect established to database: ${ process.env.MONGODB_URL }` );
}
}
);
mongooseInstance_
.then(()=>{
console.log( `Connect established to database: ${ process.env.MONGODB_URL }` );
})
.catch(( err )=>{
console.error( `Cannot connect to database: ${ process.env.MONGODB_URL }` );
});
process.on( 'exit', async ()=>{
const dbClient = await mongooseInstance_;
dbClient.disconnect();
......
......@@ -50,6 +50,10 @@ app.use(errorRoutes);
__ful();
}).on( 'error', rej__);
});
process.on( 'SIGINT', ()=>{
process.exit( 2 );
});
}catch( err ){
console.error( err );
process.exit( 1 );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment