Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

readMembers and registerMember functions added. #26

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions apiFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const write = require('./jsonRW.js');

module.exports = (app)=> {
app.post('/api/v1/members/register', (req, res) => {
let body = req.body;
if (body.userName==undefined||body.email==undefined||body.password==undefined){
res.status(400).send("Invalid Post request")
return;
}
write.registerMember(body.userName,body.email,body.password);
res.status(200).send("User Registered.");
});
}
26 changes: 16 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
/**
* External libraries
*/
const
express = require('express'),
http = require('http'),
app = express(),
bodyParser = require('body-parser'),
// When running the server can pass in the PORT as an argument (environment variable)
port = process.env.PORT || 3000;
const express = require('express');
const bodyParser = require('body-parser');

const routes = require('./apiFunctions.js');
const port = process.env.PORT || 3000; //env port for heroku and similar services.

let app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));

routes(app);

app.listen(port, () => {
console.log(`Server is open on port ${port}`);
});
27 changes: 27 additions & 0 deletions jsonRW.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const fs = require('fs');

let registerMember = (userName,email,password)=>{
let members = readMembers();
let memberDetails ={
id : (members.length)+1,
userName,
email,
password
}
members.push(memberDetails);
fs.writeFileSync('./config/members.json', JSON.stringify(members));
}

let readMembers = () => {
try {
let memberString = fs.readFileSync('./config/members.json');
return JSON.parse(memberString);
} catch (e) {
return [];
}
};

module.exports ={
registerMember,
readMembers
}