refactored the app export. Update readme.
This commit is contained in:
45
src/app.js
45
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;
|
||||
}
|
||||
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;
|
@ -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.'
|
||||
|
@ -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
|
||||
});
|
||||
|
10
src/index.js
10
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;
|
||||
})();
|
Reference in New Issue
Block a user