Skip to content

Commit

Permalink
server.js and apiRouter.js updated
Browse files Browse the repository at this point in the history
  • Loading branch information
jcuetos97 committed Sep 1, 2022
1 parent edc91e4 commit d168de6
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 20 deletions.
7 changes: 1 addition & 6 deletions db/db.json
Original file line number Diff line number Diff line change
@@ -1,6 +1 @@
[
{
"title":"Test Title",
"text":"Test text"
}
]
[{"title":"Test Title","text":"Test text"},{"title":"1","text":"1","id":"cu7ytbij0l7jc2ctz"},{"title":"1","text":"1","id":"cu7ytb2q8l7jc4gmq"},{"title":"Note","text":"Test","id":"cu7ytbglol7jc7i9i"},{"title":"1","text":"1","id":"cu7ytbad4l7jchvtz"},{"title":"1","text":"1","id":"cu7ytb1swl7jcsnsu"},{"title":"1","text":"1","id":"cu7ytb75sl7jcweqx"},{"title":"Hola","text":"Hola","id":"cu7ytbfk8l7jh0d3o"},{"title":"Hola","text":"1","id":"cu7ytbi7cl7ji75r6"}]
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"author": "Juan José Espinosa",
"license": "ISC",
"dependencies": {
"express": "^4.16.4"
"express": "^4.16.4",
"uniqid": "^5.4.0"
}
}
5 changes: 3 additions & 2 deletions public/assets/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ const saveNote = (note) =>
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(note),
});
body: JSON.stringify(note),
});

const deleteNote = (id) =>
fetch(`/api/notes/${id}`, {
Expand Down Expand Up @@ -118,6 +118,7 @@ const handleRenderSaveBtn = () => {

// Render the list of note titles
const renderNoteList = async (notes) => {
console.log("rendering");
let jsonNotes = await notes.json();
if (window.location.pathname === '/notes') {
noteList.forEach((el) => (el.innerHTML = ''));
Expand Down
2 changes: 2 additions & 0 deletions public/notes.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@
<script src="./assets/js/index.js"></script>
</body>
</html>


47 changes: 47 additions & 0 deletions routes/apiRoute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const api = require('express').Router();
const path = require ('path');
const fs = require ('fs');
var uniqid = require('uniqid');


api.get('/notes', (req,res) => {
console.log(`${req.method} request received.`);
res.sendFile(path.join(__dirname, "../db/db.json"));
});

api.post('/notes', (req,res) => {
console.log(`${req.method} request received.`);
let database = JSON.parse(fs.readFileSync('./db/db.json', "utf8"));

let userNote = {
title: req.body.title,
text: req.body.text,
id: uniqid(),
};

database.push(userNote);
fs.writeFileSync('./db/db.json', JSON.stringify(database))
console.log(database);
res.json(database);

});

api.delete('/notes/:id', (req,res) => {

let database = JSON.parse(fs.readFileSync('./db/db.json', "utf8"));

if (req.params.id) {
const id = req.params.id;
}

database.push(userNote);
fs.writeFileSync('./db/db.json', JSON.stringify(database))
console.log(database);
res.json(database);

});




module.exports = api;
23 changes: 12 additions & 11 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const express = require ('express');
const path = require ('path');
const api = require('./routes/apiRoute.js');

const PORT = 3001;
const PORT = process.env.PORT || 3001;

const app = express();

app.use(express.static('public'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));


app.use('/api', api);

app.get('/', (req,res) =>
res.sendFile(path.join(__dirname, '/public/index.html'))
);

app.get('/notes', (req,res) =>
res.sendFile()
);

app.get('/api/notes', (req,res) =>
console.info(`GET /api/reviews`)
app.get('/notes', (req, res) =>
res.sendFile(path.join(__dirname, '/public/notes.html'))
);

app.post('/api/notes', (req,res) =>
console.info(`GET /api/reviews`)
app.get('*', (req, res) =>
res.sendFile(path.join(__dirname, '/public/index.html'))
);

app.listen(PORT, () =>
Expand Down

0 comments on commit d168de6

Please sign in to comment.