diff --git a/.github/workflows/CI-CD.yml b/.github/workflows/CI-CD.yml new file mode 100644 index 00000000..b6a49a16 --- /dev/null +++ b/.github/workflows/CI-CD.yml @@ -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 diff --git a/02-nodejs/authenticationServer.js b/02-nodejs/authenticationServer.js index c5278b94..2d1a4a4e 100644 --- a/02-nodejs/authenticationServer.js +++ b/02-nodejs/authenticationServer.js @@ -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; + \ No newline at end of file diff --git a/02-nodejs/fileServer.js b/02-nodejs/fileServer.js index 82e7719b..b1005364 100644 --- a/02-nodejs/fileServer.js +++ b/02-nodejs/fileServer.js @@ -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; + \ No newline at end of file diff --git a/02-nodejs/todoServer.js b/02-nodejs/todoServer.js index cffc7d60..5c741057 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/todoServer.js @@ -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; + \ No newline at end of file