From 16ff8f5835de84108c8feda28d983e1a26dee099 Mon Sep 17 00:00:00 2001 From: kapy95 <41288942+kapy95@users.noreply.github.com> Date: Thu, 5 Dec 2019 10:16:17 +0100 Subject: [PATCH 1/4] Installed nedb as a package --- Dockerfile | 14 +++++++++++ index.js | 15 +++++------ package-lock.json | 64 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 +++--- 4 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1100be3 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,14 @@ +FROM node:9-alpine + +WORKDIR / app + +COPY package.json . +COPY package-lock.json . + +RUN npm install + +COPY index.js . + +EXPOSE 3000 + +CMD npm start \ No newline at end of file diff --git a/index.js b/index.js index c7cb29c..a0e179c 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,14 @@ var express=require('express'); var bodyParser=require('body-parser'); -var DataStore=require('nedb'); +var dataStore=require('nedb'); var port= 3000; var BASE_API_PATH="/api/v1"; +db = new Datastore({ + filename: '/index.js', + autoload: true + }); + var DB_FILE_NAME=__dirname+"/contacts.json"; console.log("Starting API Server..."); @@ -11,10 +16,6 @@ console.log("Starting API Server..."); var app= express(); app.use(bodyParser.json()); -var db= new DataStore({ - filename:DB_FILE_NAME, - autoload: true -}) app.get("/", (req,res)=>{ @@ -26,7 +27,7 @@ app.get("/", (req,res)=>{ app.get(BASE_API_PATH+"/contacts", (req,res)=>{ console.log(Date()+"- GET/contacts"); - db.find({}, (err,contacts)=>{ + /*db.find({}, (err,contacts)=>{ if(err){ console.log(Date()+"-"+err) res.sendStatus(500) @@ -35,7 +36,7 @@ app.get(BASE_API_PATH+"/contacts", (req,res)=>{ } }); -}); +*/}); app.post(BASE_API_PATH+"/contacts", (req,res)=>{ diff --git a/package-lock.json b/package-lock.json index e46fe7e..8adc604 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,19 @@ "resolved": "https://registry.npmjs.org/array-flatten/-/array-flatten-1.1.1.tgz", "integrity": "sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=" }, + "async": { + "version": "0.2.10", + "resolved": "https://registry.npmjs.org/async/-/async-0.2.10.tgz", + "integrity": "sha1-trvgsGdLnXGXCMo43owjfLUmw9E=" + }, + "binary-search-tree": { + "version": "0.2.5", + "resolved": "https://registry.npmjs.org/binary-search-tree/-/binary-search-tree-0.2.5.tgz", + "integrity": "sha1-fbs7IQ/coIJFDa0jNMMErzm9x4Q=", + "requires": { + "underscore": "~1.4.4" + } + }, "body-parser": { "version": "1.19.0", "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.19.0.tgz", @@ -182,6 +195,11 @@ "safer-buffer": ">= 2.1.2 < 3" } }, + "immediate": { + "version": "3.0.6", + "resolved": "https://registry.npmjs.org/immediate/-/immediate-3.0.6.tgz", + "integrity": "sha1-nbHb0Pr43m++D13V5Wu2BigN5ps=" + }, "inherits": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.3.tgz", @@ -192,6 +210,22 @@ "resolved": "https://registry.npmjs.org/ipaddr.js/-/ipaddr.js-1.9.0.tgz", "integrity": "sha512-M4Sjn6N/+O6/IXSJseKqHoFc+5FdGJ22sXqnjTpdZweHK64MzEPAyQZyEU3R/KRv2GLoa7nNtg/C2Ev6m7z+eA==" }, + "lie": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/lie/-/lie-3.1.1.tgz", + "integrity": "sha1-mkNrLMd0bKWd56QfpGmz77dr2H4=", + "requires": { + "immediate": "~3.0.5" + } + }, + "localforage": { + "version": "1.7.3", + "resolved": "https://registry.npmjs.org/localforage/-/localforage-1.7.3.tgz", + "integrity": "sha512-1TulyYfc4udS7ECSBT2vwJksWbkwwTX8BzeUIiq8Y07Riy7bDAAnxDaPU/tWyOVmQAcWJIEIFP9lPfBGqVoPgQ==", + "requires": { + "lie": "3.1.1" + } + }, "media-typer": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/media-typer/-/media-typer-0.3.0.tgz", @@ -225,11 +259,36 @@ "mime-db": "1.42.0" } }, + "minimist": { + "version": "0.0.8", + "resolved": "https://registry.npmjs.org/minimist/-/minimist-0.0.8.tgz", + "integrity": "sha1-hX/Kv8M5fSYluCKCYuhqp6ARsF0=" + }, + "mkdirp": { + "version": "0.5.1", + "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.1.tgz", + "integrity": "sha1-MAV0OOrGz3+MR2fzhkjWaX11yQM=", + "requires": { + "minimist": "0.0.8" + } + }, "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, + "nedb": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/nedb/-/nedb-1.8.0.tgz", + "integrity": "sha1-DjUCzYLABNU1WkPJ5VV3vXvZHYg=", + "requires": { + "async": "0.2.10", + "binary-search-tree": "0.2.5", + "localforage": "^1.3.0", + "mkdirp": "~0.5.1", + "underscore": "~1.4.4" + } + }, "negotiator": { "version": "0.6.2", "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", @@ -355,6 +414,11 @@ "mime-types": "~2.1.24" } }, + "underscore": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.4.4.tgz", + "integrity": "sha1-YaajIBBiKvoHljvzJSA88SI51gQ=" + }, "unpipe": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", diff --git a/package.json b/package.json index f500277..f644f79 100644 --- a/package.json +++ b/package.json @@ -5,9 +5,9 @@ "main": "index.js", "repository": { "type": "git", - "url":"https://github.com/kapy95/repositorio.git" + "url": "https://github.com/kapy95/repositorio.git" }, - "homepage":"https://github.com/kapy95/repositorio", + "homepage": "https://github.com/kapy95/repositorio", "scripts": { "start": "node index.js", "test": "echo \"Error: no test specified\" && exit 1" @@ -15,6 +15,7 @@ "author": "Carlos", "license": "ISC", "dependencies": { - "express": "^4.17.1" + "express": "^4.17.1", + "nedb": "^1.8.0" } } From 314d8bfc788b7638348c3c4c9d091e81f3b1f4a3 Mon Sep 17 00:00:00 2001 From: kapy95 <41288942+kapy95@users.noreply.github.com> Date: Thu, 5 Dec 2019 10:58:34 +0100 Subject: [PATCH 2/4] Added support for Post with nedb --- .gitignore | 3 ++- index.js | 25 +++++++++++++++---------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/.gitignore b/.gitignore index b19a0dc..928c09c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules/ -.devcontainer \ No newline at end of file +.devcontainer +contacts.json diff --git a/index.js b/index.js index a0e179c..dfd6859 100644 --- a/index.js +++ b/index.js @@ -1,21 +1,25 @@ +//Declaracion de dependencias: var express=require('express'); var bodyParser=require('body-parser'); -var dataStore=require('nedb'); +var Datastore=require('nedb'); var port= 3000; var BASE_API_PATH="/api/v1"; - -db = new Datastore({ - filename: '/index.js', - autoload: true - }); - var DB_FILE_NAME=__dirname+"/contacts.json"; + console.log("Starting API Server..."); + + var app= express(); app.use(bodyParser.json()); +//Inicializamos la base de datos: +db = new Datastore({ + filename: DB_FILE_NAME, //nombre del fichero que hemos definido anteriormente + autoload: true + }); + app.get("/", (req,res)=>{ @@ -27,7 +31,8 @@ app.get("/", (req,res)=>{ app.get(BASE_API_PATH+"/contacts", (req,res)=>{ console.log(Date()+"- GET/contacts"); - /*db.find({}, (err,contacts)=>{ + + db.find({}, (err,contacts)=>{ if(err){ console.log(Date()+"-"+err) res.sendStatus(500) @@ -36,14 +41,14 @@ app.get(BASE_API_PATH+"/contacts", (req,res)=>{ } }); -*/}); +}); app.post(BASE_API_PATH+"/contacts", (req,res)=>{ console.log(Date()+"- POST/contacts"); var contact=req.body; - db.insert(contact,(err)=>{ + db.insert(contact,(err)=>{ //insertamos un nuevo elemento en la base if(err){ console.log(Date()+"-"+ err); res.sendStatus(500); From d58c45026a4b539840d3f23a56398d2c8bb3867f Mon Sep 17 00:00:00 2001 From: kapy95 <41288942+kapy95@users.noreply.github.com> Date: Thu, 5 Dec 2019 11:02:52 +0100 Subject: [PATCH 3/4] Added support for method GET --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index dfd6859..768e47b 100644 --- a/index.js +++ b/index.js @@ -32,7 +32,7 @@ app.get(BASE_API_PATH+"/contacts", (req,res)=>{ console.log(Date()+"- GET/contacts"); - db.find({}, (err,contacts)=>{ + db.find({}, (err,contacts)=>{//hace una consulta de cualquier elemento gracias a {} if(err){ console.log(Date()+"-"+err) res.sendStatus(500) From 484aeddc21bed178578ec5907993835a57f45180 Mon Sep 17 00:00:00 2001 From: kapy95 <41288942+kapy95@users.noreply.github.com> Date: Thu, 5 Dec 2019 11:13:00 +0100 Subject: [PATCH 4/4] FIxed error with IDs --- index.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 768e47b..a034104 100644 --- a/index.js +++ b/index.js @@ -37,7 +37,12 @@ app.get(BASE_API_PATH+"/contacts", (req,res)=>{ console.log(Date()+"-"+err) res.sendStatus(500) }else{ - res.send(contacts); + res.send(contacts.map((contact)=>{ + + delete contact._id; + return contact; + }) + ); } });