From e4b10fe3bbe7a4787606b8743eb204593b28053a Mon Sep 17 00:00:00 2001 From: Brendan Deere Date: Wed, 26 Oct 2022 18:06:04 -0400 Subject: [PATCH 1/2] Refactor index so app can be exported I wanted to start a console with our app loaded for easier debugging of stuffs. To do this I need app to be available for export without starting the server. This commit moves the app definition into its own file which can be required separately --- src/app.js | 18 ++++++++++++++++++ src/index.js | 16 ++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) create mode 100644 src/app.js diff --git a/src/app.js b/src/app.js new file mode 100644 index 0000000..7e74e97 --- /dev/null +++ b/src/app.js @@ -0,0 +1,18 @@ +import cors from 'express' +import express from 'express' +import sqlite from 'sqlite3' +import ejs from 'ejs' + +const db = new sqlite.Database('../db.sqlite') + +const app = express() + +app.db = db + +app.set('view engine', 'ejs') + +app.get('/', function(request, response) { + response.render('index') +}) + +export default app diff --git a/src/index.js b/src/index.js index 24e934a..845152c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,21 +1,9 @@ -import cors from 'express' -import express from 'express' -import sqlite from 'sqlite3' -import ejs from 'ejs' +import app from './app.js' -const db = new sqlite.Database('../db.sqlite') - -const app = express() const port = 4004 -app.set('view engine', 'ejs') - -app.get('/', function(request, response) { - response.render('index') -}) - app.listen(port, function() { console.log(`Listening on port ${port}`) }) -db.close() +app.db.close() From 8663fe29dde54760b06fb2daf56dc16de879af25 Mon Sep 17 00:00:00 2001 From: Brendan Deere Date: Wed, 26 Oct 2022 18:07:35 -0400 Subject: [PATCH 2/2] Add yarn console This is a command that will start a REPL. The app can be loaded into the repl context with: import('') --- bin/console.js | 6 ++++++ package.json | 3 ++- 2 files changed, 8 insertions(+), 1 deletion(-) create mode 100644 bin/console.js diff --git a/bin/console.js b/bin/console.js new file mode 100644 index 0000000..b7cceb6 --- /dev/null +++ b/bin/console.js @@ -0,0 +1,6 @@ +import repl from 'repl' +import app from '../src/app.js' + +Object.assign(repl.start().context, { + app, +}) diff --git a/package.json b/package.json index 3289ee3..3f003af 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,7 @@ "sqlite3": "^5.1.1" }, "scripts": { - "server": "node src/index" + "server": "node src/index", + "console": "node bin/console" } }