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

assigment 2 completed #339

Open
wants to merge 2 commits into
base: main
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
20 changes: 20 additions & 0 deletions .github/workflows/CI-CD.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: CI/CD Pipeline

on:
pull_request:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v2

- name: Install Dependencies
run: npm install

- name: Run Tests
run: npm run test-all
83 changes: 77 additions & 6 deletions 02-nodejs/authenticationServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,80 @@
Testing the server - run `npm run test-authenticationServer` command in terminal
*/

const express = require("express")
const PORT = 3000;
const app = express();
// write your logic here, DONT WRITE app.listen(3000) when you're running tests, the tests will automatically start the server

module.exports = app;
const express = require("express")
const PORT = 3000;
const app = express();
// write your logic here, DONT WRITE app.listen(3000) when you're running tests, the tests will automatically start the server

var users = [];

app.use(express.json());
app.post("/signup", (req,res) => {
var user = req.body;
let userAlreadyExists = false;
for (var i = 0; i < user.length; i += 1) {
if(users[i].email === user.email) {
userAlreadyExists = true;
break;
}
}
if(userAlreadyExists) {
res.sendStatus(400);
} else {
users.push(user);
res.status(201).send("Signup successful");
}
});

app.post("/login", (req,res) => {
var user = req.body;
let userFound = null;
for (var i = 0; i < users.length; i += 1) {
if(users[i].username === user.username && users[i].password === user.password) {
userFound = users[i];
break;
}
}
if(userFound) {
res.json({
firstName: userFound.firstName,
lastName: userFound.lastName,
email: userFound.email
});
} else {
res.sendStatus(401).send("Unauthorized user");
}
});

app.get("/data", (req,res) => {
var email = req.headers.email;
var password = req.headers.password;
let userFound = false;
for (var i = 0; i < users.length; i += 1) {
if(users[i].email === email && users[i].password === password) {
userFound = true;
}
}

if(userFound) {
let usersToReturn = []
for (var i = 0; i < users.length; i += 1) {
usersToReturn.push({
firstName: users[i].firstName,
lastName: users[i].lastName,
email: users[i].email
});
}
res.json({
users
});
} else {
res.sendStatus(401);
}

});
// app.listen(PORT, () => {
// console.log(`Server is running on port ${PORT}`);
// })
module.exports = app;

39 changes: 32 additions & 7 deletions 02-nodejs/fileServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,35 @@

Testing the server - run `npm run test-fileServer` command in terminal
*/
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();


module.exports = app;
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();

app.get('/files', (req,res) => {
fs.readdir(path.join(__dirname, './files/'), (err,files) => {
if(err) {
return res.status(500).json({error: 'Failed to retrive files'});
}
res.json(files);
});
})

app.get('/file/:filename', (req,res) => {
const filepath = path.join(__dirname, './files/', req.params.filename);

fs.readFile(filepath, 'utf-8', (err, data) => {
if(err) {
return res.status(404).send('File not found');
}
res.send(data);
});
});

app.all('*', (req,res) => {
res.status(404).send('Route not found');
});


module.exports = app;

68 changes: 60 additions & 8 deletions 02-nodejs/todoServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,63 @@

Testing the server - run `npm run test-todoServer` command in terminal
*/
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());

module.exports = app;
const express = require('express');
const bodyParser = require('body-parser');

const app = express();

app.use(bodyParser.json());

let todos = []

app.get('/todos', (req, res) => {
res.json(todos);
});

app.get('/todos/:id', (req, res) => {
const todo = todos.find(t => t.id === parseInt(req.params.id));
if(!todo) {
res.status(404).send();
} else {
res.json(todo);
}
});

app.post('/todos', (req, res) => {
const newTodo = {
id: Math.floor(Math.random()*1000000),
title: req.body.title,
description: req.body.description
};
todos.push(newTodo);
res.status(201).json(newTodo);
});

app.put('/todos/:id', (req, res) => {
const todoIndex = todos.findIndex(t => t.id === parseInt(req.params.id));
if(todoIndex === -1) {
res.status(404).send();
} else {
todos[todoIndex].title = req.body.title;
todos[todoIndex].description = req.body.description;
res.json(todos[todoIndex]);
}
});

app.delete('/todos/:id', (req, res) => {
const todoIndex = todos.findIndex(t => t.id === parseInt(req.params.id));
if(todoIndex === -1) {
res.status(404).send();
} else {
todos.splice(todoIndex, 1);
res.status(200).send();
}
});

// for all other routes, return 404
app.use((req, res, next) => {
res.status(404).send();
});

module.exports = app;