generated from jonty-terrence/react-project-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attempt to add user data to JSON File - WIP
- Loading branch information
1 parent
bf43571
commit b856bcd
Showing
7 changed files
with
48 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import consume from './requestor' | ||
import request from 'superagent' | ||
|
||
export function addToFile (addedData) { | ||
return consume('/', 'post', addedData) | ||
.then(res => { | ||
return res.body | ||
}) | ||
const dataUrl = 'http://localhost:3000/api/v1' | ||
|
||
export function addToFile (data) { | ||
return request | ||
.post(dataUrl) | ||
.send(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
[object Object] | ||
[object Object][object Object][object Object][object Object] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,41 @@ | ||
const fs = require('fs') | ||
const express = require('express') | ||
|
||
const path = require('path') | ||
|
||
const router = express.Router() | ||
|
||
const filePath = path.join(__dirname, './metaData.json') | ||
|
||
function add (data, callback) { | ||
// console.log(lemon) | ||
// read the file | ||
fs.readFile(filePath, 'utf8', (err, contents) => { | ||
// handle file read error | ||
if(err) return callback(err) | ||
// JSON.parse the contents | ||
const lemons = JSON.parse(contents) | ||
// add the lemon (push) | ||
lemons.push(lemon) | ||
// stringify the array | ||
const newContents = JSON.stringify(lemons, null, 2) | ||
// save/write the file | ||
fs.writeFile(filePath, newContents, callback) | ||
}) | ||
} | ||
|
||
router.post('/', (req, res) => { | ||
const { url, title, description } = req.body | ||
const newData = { | ||
url: url, | ||
title: title, | ||
description: description | ||
} | ||
return fs.appendFile('./metaData.json', newData, (err) => { | ||
if (err) throw err | ||
console.log('Added to file successfully') | ||
}) | ||
const { url, title, description } = req.body | ||
const lemon = { url, title, description } | ||
add(lemon, (err) => { | ||
// sending err.message is a bad security practice - should be sanitised | ||
if(err) return res.status(500).send(err.message) | ||
res.redirect('/') | ||
}) | ||
fs.readFile(filePath, 'utf8', (err, contents) => { | ||
if (err) return res.status(500).send(err.message) | ||
console.log(JSON.parse(contents)) | ||
}) | ||
}) | ||
|
||
module.exports = router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters