diff --git a/README.md b/README.md new file mode 100644 index 0000000..ce8aa70 --- /dev/null +++ b/README.md @@ -0,0 +1,40 @@ +# README.md Generator for Node.js Application + +## Setting up the Application + +To set up this Node.js application, follow the instructions below: + +1. **Copy `.env.example` to `.env** + Open your terminal and navigate to your project's root directory. + + ```bash + $ cp .env.example .env + ``` + +2. Modify the `.env` file if you wish. The `DB_URL` should already be set +correctly. + +3. Start the postgres database: + + ```shell + $ docker-compose up -d + ``` + +4. Install the packages + + ```shell + $ npm i + ``` + +5. Run the tests + + ```shell + $ npm run test + ``` + +6. Start the Express application. + + ```shell + $ npm run serve + ``` + diff --git a/package.json b/package.json index bcfc324..00ee90a 100644 --- a/package.json +++ b/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "serve": "node ./src/index.js", - "test": "node --experimental-vm-modules node_modules/.bin/jest" + "test": "node --experimental-vm-modules node_modules/.bin/jest --detectOpenHandles --force-exit" }, "type": "module", "keywords": [], diff --git a/src/app.js b/src/app.js index 4ef9d5b..1dd889b 100644 --- a/src/app.js +++ b/src/app.js @@ -1,6 +1,6 @@ import express from 'express' -import { db } from './db'; +import db from './db.js'; /* * utility function to either get the value in the post data @@ -15,28 +15,25 @@ function valOrMissing(data, key, res) { } -export function createApp() { +// Creating an instance of Express +const app = express(); +app.use(express.json()) // for parsing application/json - // Creating an instance of Express - const app = express(); - app.use(express.json()) // for parsing application/json - - app.put("/note", async (req, res) => { - const title = valOrMissing(req.body, "title", res); - if (!title) return; - const text = valOrMissing(req.body, "text", res); - if (!text) return; - const created = req.body["created"] ? Date.parse(req.body["created"]) : new Date(); - const id = await db("note").insert({ - title, text, created - }, ["id"]) - return res.status(202).send({ "note": id[0].id }); - }); +app.put("/note", async (req, res) => { + const title = valOrMissing(req.body, "title", res); + if (!title) return; + const text = valOrMissing(req.body, "text", res); + if (!text) return; + const created = req.body["created"] ? Date.parse(req.body["created"]) : new Date(); + const id = await db("note").insert({ + title, text, created + }, ["id"]) + return res.status(202).send({ "note": id[0].id }); +}); - app.get("/note/:id", async (req, res, next) => { - const note = await db.from("note").where({ id: req.params.id }); - return res.status(200).send({ "note": note[0] }); - }); - - return app; -} \ No newline at end of file +app.get("/note/:id", async (req, res, next) => { + const note = await db.from("note").where({ id: req.params.id }); + return res.status(200).send({ "note": note[0] }); +}); + +export default app; \ No newline at end of file diff --git a/src/app.spec.js b/src/app.spec.js index 5f9c79a..47cb56f 100644 --- a/src/app.spec.js +++ b/src/app.spec.js @@ -1,9 +1,6 @@ -import { db } from "./db"; -import { createApp } from "./app"; +import app from "./app"; import request from "supertest"; -const app = createApp(); - const note = { title: 'Test Note', text: 'This is a test note.' diff --git a/src/db.js b/src/db.js index ff2634a..277d17b 100644 --- a/src/db.js +++ b/src/db.js @@ -7,7 +7,7 @@ if (!process.env.DB_URL) { }; // Creating an instance of Knex -export const db = knex({ +export default knex({ client: 'postgres', connection: process.env.DB_URL }); diff --git a/src/index.js b/src/index.js index 1a82876..14e7afb 100644 --- a/src/index.js +++ b/src/index.js @@ -1,13 +1,9 @@ // Importing express -import { createApp } from './app'; + +import app from "./app.js"; (() => { - const app = createApp(); - // Start the Express server app.listen(3000, () => { console.log('Server started on port 3000'); }); -})(); - -// Exporting the app -export default app; \ No newline at end of file +})(); \ No newline at end of file