diff --git a/02-nodejs/authenticationServer.js b/02-nodejs/authenticationServer.js index c5278b94..189fb1d2 100644 --- a/02-nodejs/authenticationServer.js +++ b/02-nodejs/authenticationServer.js @@ -29,9 +29,78 @@ 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 + 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 { + var user = req.body; + let userFound = null; + for (var i = 0; i { + var email = req.headers.email; + var password = req.headers.password; + let userFound = false; + for (var i = 0; i { + fs.readdir(path.join(__dirname, './files/'), (err, files) => { + if (err) { + return res.status(500).json({ error: 'Failed to retrieve files' }); + } + res.json(files); + }); +}) + +app.get('/file/:filename', (req, res) => { + const filepath = path.join(__dirname, './files/', req.params.filename); + + fs.readFile(filepath, 'utf8', (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'); +}); + +// app.listen(port, () => { +// console.log(`App listening on port ${port}`) +// }) module.exports = app; diff --git a/02-nodejs/todoServer.js b/02-nodejs/todoServer.js index cffc7d60..e59d1334 100644 --- a/02-nodejs/todoServer.js +++ b/02-nodejs/todoServer.js @@ -41,9 +41,82 @@ */ const express = require('express'); const bodyParser = require('body-parser'); +const fs = require('fs'); + +const port = 3000; const app = express(); +function middleware1 (req, res, next){ + fs.readFile('todos.json', 'utf8', (err, data) => { + if (err) throw err; + req.todos = JSON.parse(data); + next(); + }) +} + app.use(bodyParser.json()); +app.get('/todos', middleware1, (req, res) => { + return res.send(req.todos) +}) + +app.get('/todos/:id',middleware1, (req, res) => { + const todoIndex = parseInt(req.params.id); + const todo = req.todos.find(todo => todo.id === todoIndex); + if (!todo) + return res.status(404).send(); + return res.json(todo); +}) + +app.post('/todos', middleware1, (req, res) => { + const newTodo = { + id: req.todos.length + 1, + title: req.body.title, + completed: req.body.completed, + description: req.body.description + }; + req.todos.push(newTodo); + fs.writeFile('todos.json', JSON.stringify(req.todos), (err) => { + if(err) throw err; + res.status(201).json(newTodo); + }) +}) + +app.put('/todos/:id', middleware1, (req, res) => { + const todoIndex = parseInt(req.params.id); + if (!req.todos[todoIndex - 1]) + return res.status(404).json({ error: 'Todo not found' }); + + req.todos[todoIndex-1].title = req.body.title; + req.todos[todoIndex-1].completed = req.body.completed; + req.todos[todoIndex-1].description = req.body.description; + + fs.writeFile('todos.json', JSON.stringify(req.todos), (err) => { + if (err) + return res.status(500).json({ error: 'Internal server error' }); + return res.status(200).json({ message: 'Todo updated successfully' }); + }); +}) + +app.delete('/todos/:id', middleware1, (req,res) => { + const todoIndex = parseInt(req.params.id); + if (!req.todos[todoIndex - 1]) + return res.status(404).json({ error: 'Todo not found' }); + + req.todos.splice(todoIndex-1,1); + fs.writeFile('todos.json', JSON.stringify(req.todos), (err) => { + if(err) throw err; + return res.status(200).json({ message: 'Todo deleted successfully' }); + }); +}) + +app.use((req, res, next) => { + res.status(404).send(); +}); + module.exports = app; + +// app.listen(port, () => { +// console.log(`App listening on port ${port}`) +// }) diff --git a/02-nodejs/todos.json b/02-nodejs/todos.json new file mode 100644 index 00000000..0637a088 --- /dev/null +++ b/02-nodejs/todos.json @@ -0,0 +1 @@ +[] \ No newline at end of file